Codeforces Round #354 (Div. 2) D. Theseus and labyrinth

来源:互联网 发布:arch linux 安装教程 编辑:程序博客网 时间:2024/05/20 23:56

【题链接】点击打开链接

【题意】一个迷宫,每个位置都有一个字符,除了*(这个代表墙,不能通过)其他的字符都代表了一种性质,在这种字符上下一步能走到哪些方向。然后还有一种操作是花费1s的时间来旋转这个矩阵(旋转90度)!让矩阵里面的每个字符旋转到对应的字符。然后给出了一个人的初始位置和目的地位置,问你这个人能不能走到目的位置,如果能,输出走到目的位置的最短时间。如果不能的话,输出-1.

【分析】去掉这个旋转操作,这就是一个基本的bfs迷宫模型,有了旋转90度之后,就是相当于多了3个同等的矩阵,只需要考虑加个维度就能解决这个问题啦!具体实现就看我的代码啦。

【吐槽】写了好久,写了130+行,看第一名的代码大概50行,这难道就是智障型选手和菊苣的天壤之别吗?

【AC代码】

//Cloud , you are ky sunshine!//I can't live without you!//You are the kost beautiful girl in the world!#include <bits/stdc++.h>using namespace std;const int maxn=1010;char maze[4][maxn][maxn];int  vis[4][maxn][maxn];int  dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//UDLRint  n,m,sx,sy,ex,ey;int  rot[maxn];struct node{    int x,y,num;    int mod;//标记在哪一个图    node(){}    node(int x,int y,int num,int mod):x(x),y(y),num(num),mod(mod){}};//旋转90度void Rotate(){    rot['+']='+';    rot['-']='|';    rot['|']='-';    rot['^']='>';    rot['>']='v';    rot['<']='^';    rot['v']='<';    rot['L']='U';    rot['U']='R';    rot['R']='D';    rot['D']='L';}//判断两个点走到bool ok_l(char c){    if(c=='+'||c=='-'||c=='<'||c=='R'||c=='U'||c=='D') return true;    return false;}bool ok_r(char c){    if(c=='+'||c=='-'||c=='>'||c=='L'||c=='U'||c=='D') return true;    return false;}bool ok_u(char c){    if(c=='+'||c=='|'||c=='^'||c=='L'||c=='R'||c=='D') return true;    return false;}bool ok_d(char c){    if(c=='+'||c=='|'||c=='v'||c=='L'||c=='R'||c=='U') return true;    return false;}bool legal(int x,int y){    if(x>=0&&x<n&&y>=0&&y<m) return true;    return false;}bool ok(node now,node nxt,int t,int op){    char a=maze[t][now.x][now.y],b=maze[t][nxt.x][nxt.y];    if(op==0){        if(ok_u(a)&&ok_d(b)) return true;        return false;    }    else if(op==1){        if(ok_d(a)&&ok_u(b)) return true;        return false;    }    else if(op==2){        if(ok_l(a)&&ok_r(b)) return true;        return false;    }    else if(op==3){        if(ok_l(b)&&ok_r(a)) return true;        return false;    }    else{        return false;    }}int bfs(){    queue<node>que;    memset(vis,0,sizeof(vis));    node now,nxt;    now.x=sx,now.y=sy;    now.mod=0,now.num=0;    que.push(now);    vis[0][sx][sy]=1;    while(!que.empty()){        now=que.front();        que.pop();        if(now.x==ex&&now.y==ey){            cout<<now.num<<endl;            return 1;        }        if(!vis[(now.mod+1)%4][now.x][now.y]){            que.push(node(now.x,now.y,now.num+1,(now.mod+1)%4));            vis[(now.mod+1)%4][now.x][now.y]=1;        }        for(int i=0; i<4; i++){            nxt=now;            nxt.num=now.num+1;            nxt.x=now.x+dir[i][0];            nxt.y=now.y+dir[i][1];            if(maze[nxt.mod][nxt.x][nxt.y]=='*')    continue;            if(vis[nxt.mod][nxt.x][nxt.y]) continue;            if(!legal(nxt.x,nxt.y))        continue;            if(!ok(now,nxt,now.mod,i))     continue;            que.push(nxt);            vis[nxt.mod][nxt.x][nxt.y]=true;        }    }    return 0;}int main(){    Rotate();    cin>>n>>m;    for(int i=0; i<n; i++) cin>>maze[0][i];    for(int k=1; k<4; k++){        for(int i=0; i<n; i++){            for(int j=0; j<m; j++){                maze[k][i][j]=rot[maze[k-1][i][j]];            }        }    }    cin>>sx>>sy;    cin>>ex>>ey;    sx--,sy--;    ex--,ey--;    //cout<<sx<<" "<<sy<<" "<<ex<<" "<<ey<<endl;    if(sx==ex&&sy==ey){        puts("0");        return 0;    }    else{//        int ans=bfs()        if(!bfs()){            puts("-1");        }    }    return 0;}


1 0
原创粉丝点击