Codeforces #354D (Div. 2) 暴力BFS

题目链接:题目

#include <bits/stdc++.h>

using namespace std;

int n,m;
char mp[1005][1005];
int x1,x2,y1,y2;

struct node{
    int x,y,step,tim;
    node(int X,int Y,int Z,int T):x(X),y(Y),step(Z),tim(T){};
};

vector<pair<int,int> > D[15];
int vis[1005][1005][4];
int dir[4][2]={{-1,0},{1,0},{0,1},{0,-1}};

int getD(node x){
    if(x.x<1||x.x>n||x.y<1||x.y>m) return -1;
    char c=mp[x.x][x.y];
    if(c=='+'){
        return 0;
    }
    else if(c=='-'){
        if(x.step%2==1) return 2;
        else return 1;
    }
    else if(c=='|'){
        if(x.step%2==1) return 1;
        else return 2;
    }
    else if(c=='^'){
        if(x.step%4==0) return 3;
        else if(x.step%4==1) return 4;
        else if(x.step%4==2) return 6;
        else return 5;
    }
    else if(c=='>'){
        if(x.step%4==0) return 4;
        else if(x.step%4==1) return 6;
        else if(x.step%4==2) return 5;
        else return 3;
    }
    else if(c=='<'){
        if(x.step%4==0) return 5;
        else if(x.step%4==1) return 3;
        else if(x.step%4==2) return 4;
        else return 6;
    }
    else if(c=='v'){
        if(x.step%4==0) return 6;
        else if(x.step%4==1) return 5;
        else if(x.step%4==2) return 3;
        else return 4;
    }
    else if(c=='L'){
        if(x.step%4==0) return 7;
        else if(x.step%4==1) return 9;
        else if(x.step%4==2) return 8;
        else return 10;
    }
    else if(c=='R'){
        if(x.step%4==0) return 8;
        else if(x.step%4==1) return 10;
        else if(x.step%4==2) return 7;
        else return 9;
    }
    else if(c=='U'){
        if(x.step%4==0) return 9;
        else if(x.step%4==1) return 8;
        else if(x.step%4==2) return 10;
        else return 7;
    }
    else if(c=='D'){
        if(x.step%4==0) return 10;
        else if(x.step%4==1) return 7;
        else if(x.step%4==2) return 9;
        else return 8;
    }
    else if(c=='*'){
        return 11;
    }
}

bool OK(int a,int b,int c){
    if(a==-1||b==-1) return false;
    if(c==0)      return (b==0||b==2||b==6||b==9||b==8||b==7)&&(a==0||a==2||a==3||a==10||a==8||a==7);
    else if(c==1) return (b==0||b==2||b==3||b==10||b==8||b==7)&&(a==0||a==2||a==6||a==9||a==8||a==7);
    else if(c==2) return (b==0||b==1||b==5||b==10||b==8||b==9)&&(a==0||a==1||a==4||a==10||a==9||a==7);
    else if(c==3) return (b==0||b==1||b==4||b==9||b==10||b==7)&&(a==0||a==1||a==5||a==10||a==8||a==9);
}

int BFS(){
    queue<node> Q;
    Q.push(node(x1,y1,0,0));
    while(!Q.empty()){
        node t=Q.front();
        Q.pop();
        if(t.x<1||t.x>n||t.y<1||t.y>m) continue;
        if(t.x==x2&&t.y==y2) return t.tim;
        if(vis[t.x][t.y][t.step%4]==1)  continue;
        vis[t.x][t.y][t.step%4]=1;
        int v=getD(t);
        Q.push(node(t.x,t.y,t.step+1,t.tim+1));
        for(int i=0;i<4;i++){
            int k=getD(node(t.x+dir[i][0],t.y+dir[i][1],t.step,0));
            if(OK(v,k,i))
                Q.push(node(t.x+dir[i][0],t.y+dir[i][1],t.step,t.tim+1));
        }
    }
    return -1;
}

int main(){
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) cin>>mp[i][j];
    }
    scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
    cout<<BFS()<<endl;
    return 0;
}



题意,一个矩阵,一堆门有不同的意思,详细见题目,你可以花费一单位时间来旋转所有的门。也可以从两个全开的门之间到另一个房间。。

1000*1000的数据范围很好理解就是一个暴力搜索题目,就是代码比较恶心。

因为判断联通什么的还要旋转多少次都要各种if-----ififififif。。总之编码能力还是比较弱,并没有1a。。继续努力吧蒟蒻。。

原文地址:https://www.cnblogs.com/zhangxianlong/p/10672558.html