ZOJ-3291-Never End

来源:互联网 发布:在家赚钱 知乎 编辑:程序博客网 时间:2024/06/15 15:19
Never End

Time Limit: 2 Seconds      Memory Limit: 32768 KB

"Never End" is a flash game. Here, we consider a simplified version of it. A world is represented by a board ofN byM, where each grid may be empty or a block. The world is surrounded by blocks with one empty grid being the exit. The player can use the following four operations to escape from the world (get to the exit).

  • Step Left one grid.
  • Step Right one grid.
  • Rotate Left the world.
  • Rotate Right the world.

However, the player can not get into the blocks. After one operation, the player will fall off until he reaches a block or gets to the exit. Refer to the figure for a more intuitive understanding of the four operations.

Input

There are no more than 50 cases. Each case begins with a line with two integersN (3 <=N <= 500) and M (3 <= M <= 500), the height and width of the world respectively.The nextN lines describe the world. Each of theN lines contains exactlyM characters. A '#' denotes a block. The 'E' denotes the exit, which is guaranteed to be at one edge of the world excluding the corners. The '|' represents the player. You can assume that the player stands on a block at first.

Process to the end of the file.

Output

For each case, print a line with the minimal number of operations that the player can escape from the world. If it is impossible for the player to escape, print "Can not escape!" instead.

Sample Input

9 9########## #     ## # ### ## #  #  #E ## #  ## #  #####   ##  ##   |   ##########7 7##########|  #####  #E     #####  ####   ########

Sample Output

4Can not escape!

题目大意:给你一张地图,一个人,一个出口,问最少多少步走出去。给定每次可以有四种操作、往左走,往右走,地图向左旋转90度,地图向右旋转90度。如果走的过程中踩 空则会掉下去直到踩到石块。

题目分析:总体来说这道题目题意鲜明,很直接地告诉你怎么操作。不过新颖的一点是地图旋转。我们处理起来可以把地图不转,只考虑人的运动,具体怎么实现我想看代码更 能够理解吧。

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3648 

代码清单:

#include<map>#include<queue>#include<cmath>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;struct edge{    int x,y;    int pos;    //人脚的朝向    int step;};int N,M;int sx,sy;char s[505][505];int vis[505][505][4]; //第三维标记的是人脚的朝向int xy[4][2]={{0,-1},{-1,0},{0,1},{1,0}};bool check(int x,int y){    if(x>=0&&x<N&&y>=0&&y<M) return true;    return false;}void bfs(){    queue<edge>q;    edge p,w;    while(q.size()) q.pop();    memset(vis,0,sizeof(vis));    p.x=sx; p.y=sy; p.pos=0; p.step=0;    vis[p.x][p.y][p.pos]=1;    q.push(p);    while(q.size()){        p=q.front(); q.pop();        for(int i=0;i<4;i+=2){            w=p;            w.x+=xy[(p.pos+i)%4][0];            w.y+=xy[(p.pos+i)%4][1];            w.step+=1;            if(check(w.x,w.y)&&!vis[w.x][w.y][w.pos]&&s[w.x][w.y]!='#'){                if(s[w.x][w.y]=='E'){                    printf("%d\n",w.step);                    return ;                }                //判断是否踩空                while(check(w.x,w.y)&&s[w.x][w.y]!='#'){                    w.x+=xy[3-p.pos][0];                    w.y+=xy[3-p.pos][1];                }                w.x-=xy[3-p.pos][0];                w.y-=xy[3-p.pos][1];                if(s[w.x][w.y]=='E'){                    printf("%d\n",w.step);                    return ;                }                if(!vis[w.x][w.y][w.pos]){                    vis[w.x][w.y][w.pos]=1;                    q.push(w);                }            }            //旋转地图,把地图留着不动,考虑人的运动            w=p;            while(check(w.x,w.y)&&s[w.x][w.y]!='#'){                w.x+=xy[(p.pos+i)%4][0];                w.y+=xy[(p.pos+i)%4][1];            }            w.x-=xy[(p.pos+i)%4][0];            w.y-=xy[(p.pos+i)%4][1];            if(w.pos%2) w.pos=(w.pos+i+1)%4;   //先向右旋转再向左旋转            else        w.pos=(w.pos+i+3)%4;   //先向左旋转再向右旋转,两次旋转方式不同主要是因为位置的原因            w.step+=1;            if(s[w.x][w.y]=='E'){                printf("%d\n",w.step);                return ;            }            if(!vis[w.x][w.y][w.pos]){                vis[w.x][w.y][w.pos]=1;                q.push(w);            }        }    }    printf("Can not escape!\n");}int main(){    while(scanf("%d%d",&N,&M)!=EOF){        getchar();        for(int i=0;i<N;i++){            gets(s[i]);            for(int j=0;j<M;j++){                if(s[i][j]=='|'){                    sx=i;                    sy=j;                    s[i][j]=' ';                }            }        }        bfs();    }return 0;}


0 0