The Monocycle UVA - 10047 bfs

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 × 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 midpoint 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 midpoint 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 midpoint 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 ≤ M, N ≤ 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

题目自行去vj看把【捂脸】

就是bfs的模板  但我还是因为输出问题来了两发wa

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
int visit[30][30][4][5];
char map[30][30];
int n,m,sx,sy,ex,ey;
int dis[4][2]={-1,0,0,1,1,0,0,-1};

//0123 上 右 下 左 01234绿黑红蓝白 
struct node
{
	int x,y,d,c,t;
};

queue<node> q;
int bfs()
{
	node s,now,next;
	s.x=sx,s.y=sy,s.c=0,s.d=0,s.t=0;
	visit[sx][sy][0][0]=1;
	q.push(s);
	while(!q.empty())
	{
		now=q.front();
		if(now.x==ex&&now.y==ey&&now.c==0)
		{
			return now.t;
		}
		q.pop();
		//顺时针旋转
		next.x=now.x;
		next.y=now.y;
		next.c=now.c;
		next.t=now.t+1;
		next.d=(now.d+1)%4; 
		if(visit[next.x][next.y][next.d][next.c]==0)
		{
			visit[next.x][next.y][next.d][next.c]=1;
			q.push(next);
		}
		//逆时针 
		next.x=now.x;
		next.y=now.y;
		next.c=now.c;
		next.t=now.t+1;
		next.d=(now.d+3)%4; 
		if(visit[next.x][next.y][next.d][next.c]==0)
		{
			visit[next.x][next.y][next.d][next.c]=1;
			q.push(next);
		}
		//走	
		next.x=now.x+dis[now.d][0];
		next.y=now.y+dis[now.d][1];
		next.c=(now.c+1)%5;
		next.t=now.t+1;
		next.d=now.d; 
		if(map[next.x][next.y]=='#') continue;
		if(visit[next.x][next.y][next.d][next.c]==0&&next.x>=0&&next.x<n&&next.y<m&&next.y>=0)
		{
			visit[next.x][next.y][next.d][next.c]=1;
			q.push(next);
		}	
	}
	return 0;
}


int main()
{
	int t=0;
	while(cin>>n>>m)
	{
		if(n==0&&m==0) break;
		t++;
		while(!q.empty()) q.pop();
		memset(visit,0,sizeof(visit));
		for(int i=0;i<n;i++)
		{
			scanf("%s",map[i]);
		}
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(map[i][j]=='S')
				{
					sx=i,sy=j;
				}
				if(map[i][j]=='T')
				{
					ex=i,ey=j;
				}								
			}
		}		
		int g=bfs();
		if(t>1)
        printf("\n");
        printf("Case #%d\n",t);
        if(g)
            printf("minimum time = %d sec\n",g);
        else
            printf("destination not reachable\n");
	}
	return 0;
} 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值