uva 10047 搜索

来源:互联网 发布:国建筑现状 知乎 编辑:程序博客网 时间:2024/06/05 10:15


  Problem A: The Monocycle 

A monocycle is a cycle that runs on one wheel and the one we will be considering is a bit more special. It has a solid wheel colored with five different colors as shown in the figure:

The colored segments make equal angles (72o) at the center. A monocyclist rides this cycle on an$M \times N$ grid of square tiles. The tiles have such size that moving forward from the center of one tile to that of the next one makes the wheel rotate exactly 72o around its own center. The effect is shown in the above figure. When the wheel is at the center of square 1, the mid­point of the periphery of its blue segment is in touch with the ground. But when the wheel moves forward to the center of the next square (square 2) the mid­point of its white segment touches the ground.

Some of the squares of the grid are blocked and hence the cyclist cannot move to them. The cyclist starts from some square and tries to move to a target square in minimum amount of time. From any square either he moves forward to the next square or he remains in the same square but turns 90o left or right. Each of these actions requires exactly 1 second to execute. He always starts his ride facing north and with the mid­point of the green segment of his wheel touching the ground. In the target square, too, the green segment must be touching the ground but he does not care about the direction he will be facing.

Before he starts his ride, please help him find out whether the destination is reachable and if so the minimum amount of time he will require to reach it.

Input 

The input may contain multiple test cases.

The first line of each test case contains two integers M and N ($1 \le M$,$N \le 25$) giving the dimensions of the grid. Then follows the description of the grid inM lines ofN characters each. The character `#' will indicate a blocked square, all other squares are free. The starting location of the cyclist is marked by `S' and the target is marked by `T'. The input terminates with two zeros forM andN.

Output 

For each test case in the input first print the test case number on a separate line as shown in the sample output. If the target location can be reached by the cyclist print the minimum amount of time (in seconds) required to reach it exactly in the format shown in the sample output, otherwise, print ``destination not reachable".

Print a blank line between two successive test cases.

Sample Input 

1 3S#T10 10#S.......##..#.##.###.##.##.##.#....##.###.##..#.##..#.##...#......##...##.##...#.###...#.#.....###T0 0

Sample Output 

Case #1destination not reachable Case #2minimum time = 49 sec

这个题的难点就在于状态太多了,有方向,有颜色,还有坐标,可以把它整个作为一个整体用结构体存储,就可以解决了。下面是代码:

 

#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<cmath>#include<queue>#include<algorithm>using namespace std;const int inf=1000000000;const int maxr=30;const int maxc=30;int R,C,sr,sc,tr,tc;int ans;int d[maxr][maxc][4][5],vis[maxr][maxc][4][5];char map[maxr][maxc];const int dr[]={-1,0,1,0};const int dc[]={0,-1,0,1};struct state{     int r,c,dir,color;     state(int r,int c,int dir,int color ):r(r),c(c),dir(dir),color(color){}//初始化};queue <state> Q;void update(int r,int c,int dir,int color,int v){       if(r<0||r>=R||c<0||c>=C) return;       if(map[r][c]=='.'&&!vis[r][c][dir][color]){               Q.push(state(r,c,dir,color));               vis[r][c][dir][color]=1;               d[r][c][dir][color]=v;               if(r==tr&&c==tc&&color==0)                  ans=min(ans,v);       }}void bfs(state st){      d[st.r][st.c][st.dir][st.color]=0;      vis[st.r][st.c][st.dir][st.color]=1;      Q.push(st);      while(!Q.empty()){            st=Q.front();            Q.pop();            int v=d[st.r][st.c][st.dir][st.color]+1;            update(st.r,st.c,(st.dir+1)%4,st.color,v);//向左 ,那个式子的含义是压缩路径的,上下代表2,4,左右是1,3            update(st.r,st.c,(st.dir+3)%4,st.color,v);//向右            update(st.r+dr[st.dir],st.c+dc[st.dir],st.dir,(st.color+1)%5,v);//前进      }}int main(){    int caseno=0;    while(scanf("%d%d",&R,&C)==2&&R&&C){        for(int i=0;i<R;i++){           scanf("%s",map[i]);           for(int j=0;j<C;j++){                  if(map[i][j]=='S'){                        sr=i;                        sc=j;                  }                  if(map[i][j]=='T'){                      tr=i;                      tc=j;                  }           }        }        map[sr][sc]=map[tr][tc]='.';        ans=inf;        memset(vis,0,sizeof(vis));        bfs(state(sr,sc,0,0));        if(caseno>0) printf("\n");        printf("Case #%d\n", ++caseno);        if(ans==inf)          printf("destination not reachable\n");        else printf("minimum time = %d sec\n",ans);    }     return 0;}

 
	
				
		
原创粉丝点击