`

Uva 10047 - The Monocycle 宽度优先遍历

 
阅读更多

连接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=543&problem=988&mosmsg=Submission+received+with+ID+11654125

 

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 in M lines of N 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 for M and N.

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 3
S#T
10 10
#S.......#
#..#.##.##
#.##.##.##
.#....##.#
##.##..#.#
#..#.##...
#......##.
..##.##...
#.###...#.
#.....###T
0 0

 

Sample Output 

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

 

 

/*
是一般的宽度搜索题
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
       int x;
       int y;
       int type;
       int forward;
       int step;
}term,gold,t;
queue<node>q;
int fang[4][2]={-1,0,0,1,1,0,0,-1};
char map[27][27];
bool mask[27][27][5][4];//在一个方格同一种状态只能出现一次
int M,N;
int main()
{
      // freopen("in.txt","r",stdin);
       int T=1,flag;
       int i,j;
       while(scanf("%d%d",&M,&N),M)
       {
              while(!q.empty())q.pop();
              memset(map,0,sizeof(map));
              memset(mask,0,sizeof(mask));
              if(T==1)printf("Case #%d\n",T++);//注意输出格式
              else printf("\nCase #%d\n",T++);
              for(i=1;i<=M;i++)
              {
                     for(j=1;j<=N;j++)
                     {
                            cin>>map[i][j];
                            if(map[i][j]=='#')map[i][j]=0;
                            else if(map[i][j]=='S')
                            {
                                   term.x=i;
                                   term.y=j;
                                   term.step=0;
                                   term.type=0;
                                   term.forward=0;
                                   q.push(term);
                                   mask[i][j][0][0]=true;
                            }
                            else if(map[i][j]=='T')
                            {
                                   gold.x=i;
                                   gold.y=j;
                                   gold.type=0;
                            }
                     }
              }
              flag=0;
              while(!q.empty())
              {
                     term=q.front();
                     q.pop();
                     t.x=term.x+fang[term.forward][0];
                     t.y=term.y+fang[term.forward][1];
                     t.forward=term.forward;
                     t.step=term.step+1;
                     t.type=(term.type+1)%5;

                     if(t.x==gold.x&&t.y==gold.y&&t.type==gold.type)
                     {
                            flag=1;
                            break;
                     }
                     if(mask[t.x][t.y][t.type][t.forward]==false&&map[t.x][t.y]!=0)
                     {
                            mask[t.x][t.y][t.type][t.forward]=true;
                            //printf("t.x=%d  t.y=%d  t.forward=%d t.step=%d t.type=%d\n",t.x,t.y,t.forward,t.step,t.type);
                            q.push(t);
                     }
                     t.x=term.x;
                     t.y=term.y;
                     t.forward=(term.forward+1)%4;
                     t.step=term.step+1;
                     t.type=term.type;
                     if(mask[t.x][t.y][t.type][t.forward]==false)
                     {
                            mask[t.x][t.y][t.type][t.forward]=true;
                            //printf("t.x=%d  t.y=%d  t.forward=%d t.step=%d t.type=%d\n",t.x,t.y,t.forward,t.step,t.type);
                            q.push(t);
                     }
                     t.forward=(term.forward+3)%4;
                     if(mask[t.x][t.y][t.type][t.forward]==false)
                     {
                            mask[t.x][t.y][t.type][t.forward]=true;
                            //printf("t.x=%d  t.y=%d  t.forward=%d t.step=%d t.type=%d\n",t.x,t.y,t.forward,t.step,t.type);
                            q.push(t);
                     }
              }
              if(flag)printf("minimum time = %d sec\n",t.step);
              else printf("destination not reachable\n");
       }
       return 0;
}

 

 

 

 

 

 

1
5
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics