uva 10047 - The Monocycle bfs

3 篇文章 0 订阅

题目链接

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



Miguel Revilla 
2000-12-26

题意:一自行车的轮子被分成5个扇区,涂了5种不同颜色。自行车每1秒要么骑到下一个格子,要么左转或者右转90。。一开始自行车面向北,颜色为绿,到达目标格时,必须触底颜色为绿,但朝向无限制。求到达目标格的最短时间。
分析:自从做了网赛那道,第一次了解了bfs的状态。。。这道题就是x,y坐标加上颜色,方向一共四个状态。。。
几个注意的地方:一个是那个四个方向必须按照顺时针顺序从北开始,另一个是最后组数据不能有多余空格。。。。。
/**
 * @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;
}








Miguel Revilla 
2000-12-26
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值