Ultraviolet A 10047-the monocycle BFS

Source: Internet
Author: User

Question Link

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 monoworkflow ist rides this cycle on an 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 72OAround 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 specified ist cannot move to them. the specified ist 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 90OLeft 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 in multiple test cases.

The first line of each test case contains two integersMAndN(,) Giving the dimensions of the Grid. Then follows the description of the grid inMLinesNCharacters each. The character'#'Will indicate a blocked square, all other squares are free. The starting location of the specified ist is marked'SAnd the target is marked'T'. The input terminates with two zerosMAndN.

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 specified ist 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


Miguel Revilla
2000-12-26

The wheel of a bicycle is divided into five slices and painted in five different colors. The bicycle is either riding to the next grid every 1 second, or turning left or right 90 .. At first, the bicycle is facing north and the color is green. When the target cell is reached, the base color must be green, but the orientation is unlimited. The shortest time to reach the target.Analysis: I learned about the status of BFS for the first time since I made the online competition... This question is the X, Y coordinates plus the color, the direction of a total of four states...Note: one is that the four directions must start from the north clockwise, and the other is that the last group of data cannot contain spaces .....
/** * @author neko01 *///#pragma comment(linker, "/STACK:102400000,102400000")#include <cstdio>#include <cstring>#include <string.h>#include <iostream>#include <algorithm>#include <queue>#include <vector>#include <cmath>#include <set>#include <map>using namespace std;typedef long long LL;#define min3(a,b,c) min(a,min(b,c))#define max3(a,b,c) max(a,max(b,c))#define pb push_back#define mp(a,b) make_pair(a,b)#define clr(a) memset(a,0,sizeof a)#define clr1(a) memset(a,-1,sizeof a)#define dbg(a) printf("%d\n",a)typedef pair<int,int> pp;const double eps=1e-9;const double pi=acos(-1.0);const int INF=0x3f3f3f3f;const LL inf=(((LL)1)<<61)+5;char s[28][28];bool vis[28][28][5][4];struct node{    int x,y,c,d,step;    node(int x=0,int y=0,int c=0,int d=0,int step=0):x(x),y(y),c(c),d(d),step(step){}};int dir[4][2]={-1,0,0,1,1,0,0,-1};int n,m,sx,sy;bool immap(int x,int y){    return x>=0&&y>=0&&x<n&&y<m&&s[x][y]!='#';}int bfs(){    clr(vis);    queue<node>q;    vis[sx][sy][0][0]=true;    q.push(node(sx,sy,0,0,0));    while(!q.empty())    {        node cur=q.front();        q.pop();        int x=cur.x,y=cur.y;        if(s[x][y]=='T'&&cur.c==0) return cur.step;        for(int i=0;i<4;i++)        {            if(i==cur.d)            {                int xx=x+dir[i][0];                int yy=y+dir[i][1];                int nowc=(cur.c+1)%5;                if(immap(xx,yy)&&!vis[xx][yy][nowc][i])                {                    vis[xx][yy][nowc][i]=true;                    q.push(node(xx,yy,nowc,i,cur.step+1));                }            }            if(i==(cur.d-1+4)%4||i==(cur.d+1)%4)            {                if(!vis[x][y][cur.c][i])                {                    vis[x][y][cur.c][i]=true;                    q.push(node(x,y,cur.c,i,cur.step+1));                }            }        }    }    return -1;}int main(){    int cnt=0;    while(~scanf("%d%d",&n,&m))    {        if(n==0&&m==0) break;        for(int i=0;i<n;i++)        {            scanf("%s",s[i]);            for(int j=0;j<m;j++)            {                if(s[i][j]=='S')                    sx=i,sy=j;            }        }        if(cnt!=0) puts("");        int ans=bfs();        printf("Case #%d\n",++cnt);        if(ans==-1) puts("destination not reachable");        else printf("minimum time = %d sec\n",ans);    }    return 0;}





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 monoworkflow ist rides this cycle on an 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 72OAround 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 specified ist cannot move to them. the specified ist 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 90OLeft 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 in multiple test cases.

The first line of each test case contains two integersMAndN(,) Giving the dimensions of the Grid. Then follows the description of the grid inMLinesNCharacters each. The character'#'Will indicate a blocked square, all other squares are free. The starting location of the specified ist is marked'SAnd the target is marked'T'. The input terminates with two zerosMAndN.

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 specified ist 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


Miguel Revilla
2000-12-26

Ultraviolet A 10047-the monocycle BFS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.