The Monocycle - UVa 10047 搜索

12 篇文章 0 订阅

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

题意:在开始的时候你是绿色轮子在下面,面向北方,要求走到终点时候也是绿色轮子在下面,方向无所谓,每秒可以向前走一格,或是转90度,轮子一共五个颜色,问时间最少花费多少。

思路:无非是加了轮子颜色和方向的dfs。

AC代码如下:

#include<cstdio>
#include<cstring>
using namespace std;
char s[40][40];
int t,n,m,sx,sy,ex,ey,num[2],vis[40][40][5][5],dx[4]={-1,0,1,0},dy[4]={0,1,0,-1};
bool flag;
struct node
{
    int x,y,col,dir;
}dp[2][100010];
void start()
{
    int i,j;
    for(i=1;i<=n;i++)
       for(j=1;j<=m;j++)
       {
           if(s[i][j]=='S')
           {
               sx=i;
               sy=j;
           }
           else if(s[i][j]=='T')
           {
               ex=i;
               ey=j;
           }
       }
}
void solve(int a,int b)
{
    int i,j,k;
    for(i=1;i<=num[a];i++)
    {
        if(dp[a][i].x==ex && dp[a][i].y==ey && dp[a][i].col==0)
        {
            flag=true;
            return;
        }
        if(vis[dp[a][i].x+dx[dp[a][i].dir]][dp[a][i].y+dy[dp[a][i].dir]][(dp[a][i].col+1)%5][dp[a][i].dir]!=t
           && s[dp[a][i].x+dx[dp[a][i].dir]][dp[a][i].y+dy[dp[a][i].dir]]!='#')
        {
            vis[dp[a][i].x+dx[dp[a][i].dir]][dp[a][i].y+dy[dp[a][i].dir]][(dp[a][i].col+1)%5][dp[a][i].dir]=t;
            num[b]++;
            dp[b][num[b]].x=dp[a][i].x+dx[dp[a][i].dir];
            dp[b][num[b]].y=dp[a][i].y+dy[dp[a][i].dir];
            dp[b][num[b]].col=(dp[a][i].col+1)%5;
            dp[b][num[b]].dir=dp[a][i].dir;
        }
        if(vis[dp[a][i].x][dp[a][i].y][dp[a][i].col][(dp[a][i].dir+1)%4]!=t)
        {
            vis[dp[a][i].x][dp[a][i].y][dp[a][i].col][(dp[a][i].dir+1)%4]=t;
            num[b]++;
            dp[b][num[b]].x=dp[a][i].x;
            dp[b][num[b]].y=dp[a][i].y;
            dp[b][num[b]].col=dp[a][i].col;
            dp[b][num[b]].dir=(dp[a][i].dir+1)%4;
        }
        if(vis[dp[a][i].x][dp[a][i].y][dp[a][i].col][(dp[a][i].dir+3)%4]!=t)
        {
            vis[dp[a][i].x][dp[a][i].y][dp[a][i].col][(dp[a][i].dir+3)%4]=t;
            num[b]++;
            dp[b][num[b]].x=dp[a][i].x;
            dp[b][num[b]].y=dp[a][i].y;
            dp[b][num[b]].col=dp[a][i].col;
            dp[b][num[b]].dir=(dp[a][i].dir+3)%4;
        }
    }
}
int main()
{
    int i,j,k,x,y,a,b,step;
    while(~scanf("%d%d",&n,&m) && n+m)
    {
        t++;
        if(t!=1)
          printf("\n");
        for(i=1;i<=n;i++)
           scanf("%s",s[i]+1);
        for(i=0;i<=n+1;i++)
           s[i][0]=s[i][m+1]='#';
        for(i=0;i<=m+1;i++)
           s[0][i]=s[n+1][i]='#';
        start();
        num[0]=1;
        dp[0][1].x=sx;dp[0][1].y=sy;dp[0][1].col=0;dp[0][1].dir=0;
        flag=false;
        step=0;
        while(!flag)
        {
            step++;

            if(step&1)
            {
                a=0;b=1;
            }
            else
            {
                a=1;b=0;
            }
            //printf("%d %d\n",step,num[a]);
            if(num[a]==0)
              break;
            num[b]=0;
            solve(a,b);
        }
        printf("Case #%d\n",t);
        if(flag)
          printf("minimum time = %d sec\n",step-1);
        else
          printf("destination not reachable\n");
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值