UVA 10047 - The Monocycle

题目如下:


 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 differentcolors as shown in the figure:

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

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

Before he starts his ride, please help him find out whetherthe destination is reachable and if so the minimum amount of time he willrequire 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 ofthe grid in M lines of N characters each. The character `#' willindicate a blocked square, all other squares are free. The starting locationof 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 aseparate line as shown in the sample output. If the target location can bereached by the cyclist print the minimum amount of time (in seconds) requiredto 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

求一个点到另一个点的最短时间,明显用BFS,但限制条件比较多,要求终点的颜色和起点相同,并且车轮有方向,只能前进或左转90°或右转90°,所以用四位数组来标记,代表坐标,颜色,方向,然后正常的模拟和BFS,模拟左转,右转,前进,根据方向的不同前进的坐标变化也有所不同,所以位移数组和方向要对应起来。

AC的代码如下:

#include 
   
   
    
    

using namespace std;
int d[4][2]= {{-1,0},{0,1},{1,0},{0,-1}};
struct point
{
    int x;
    int y;
    int color;
    int direction;
    int time;
};
queue
    
    
     
      q;

int sx,sy,tx,ty,M,N;
int vis[30][30][5][5];

char graph[30][30];
int ok=0;
void bfs()
{
    while(!q.empty()) q.pop();

    point a;
    a.x=sx;
    a.y=sy;
    a.color=0;
    a.direction=0;
    a.time=0;
    q.push(a);

    vis[sx][sy][0][0]=1;
    while(!q.empty())
    {
        point u;
        u=q.front();
        q.pop();

        if(u.x==tx&&u.y==ty&&u.color==0)
        {
            cout<<"minimum time = "<
     
     
      
      <<" sec"<
      
      
       
       =1&&v.y>=1&&v.x<=M&&v.y<=N&&!vis[v.x][v.y][v.color][v.direction]&&graph[v.x][v.y]!='#')
        {
            vis[v.x][v.y][v.color][v.direction]=1;
            q.push(v);


        }
        v.x=u.x;
        v.y=u.y;
        v.direction=(u.direction+3)%4;
        v.color=u.color;
        v.time=u.time+1;
        if(v.x>=1&&v.y>=1&&v.x<=M&&v.y<=N&&!vis[v.x][v.y][v.color][v.direction]&&graph[v.x][v.y]!='#')
        {
            vis[v.x][v.y][v.color][v.direction]=1;
            q.push(v);
        }

        v.x=u.x+d[u.direction][0];
        v.y=u.y+d[u.direction][1];
        v.direction=u.direction;
        v.color=(u.color+1)%5;
        v.time=u.time+1;
        if(v.x>=1&&v.y>=1&&v.x<=M&&v.y<=N&&!vis[v.x][v.y][v.color][v.direction]&&graph[v.x][v.y]!='#')
        {
            vis[v.x][v.y][v.color][v.direction]=1;
            q.push(v);
        }


    }
    cout<<"destination not reachable"<
       
       
         >M>>N) { T++; memset(vis,0,sizeof vis); if(M==0) break; for(int i=1; i<=M; i++) { getchar(); for(int j=1; j<=N; j++) { cin>>graph[i][j]; if(graph[i][j]=='S') { sx=i; sy=j; } else if(graph[i][j]=='T') { tx=i; ty=j; } } } if(T!=1) cout< 
         
       
      
      
     
     
    
    
   
   

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值