UVA - 10047 The Monocycle(BFS + vis数组的理解)

  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 monocyclist 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 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 (,) giving the dimensions of the grid. Then follows the description of the grid inM 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


题意:
一自行车的轮子被分成5个扇区,涂了5种不同颜色。
自行车花1秒时间,骑到下一个格子。
左转或者右转90,同样需要1秒时间。

一开始自行车面向北,颜色为蓝色,到达目标格时,必须触底颜色为蓝色,但朝向无限制,求到达目标格的最短时间。

解析:
这道题使我对于vis数组的认识有了一个新的认识,以前一直认为它仅仅是标记一下到没到过一个区域,现在才知道理解错了。
vis数组是用来表示某个状态是否出现过,也就是说同一个状态不能出现两次,明白了这点,就知道vis应该是个四维数组(在一个格子里,坐标,方向,颜色)。
如何判断是否转向,可以用(dir + 1) % 4 == i 和 (dir + 3) % 4 == i判断。
弄清楚这些,只要用普通的BFS框架套进去就好了。

#include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h>
using namespace std;
               //上  左  下 右
const int dr[] = {-1, 0, 1, 0};
const int dc[] = { 0,-1, 0, 1};

const int N = 30;
const int COL = 5;
const int D = 4;

struct Node{
	int r,c;
	int dir;
	int col;
	int sec;
	Node(int r,int c,int dir,int col,int sec) {
		this->r = r;
		this->c = c;
		this->dir = dir;
		this->col = col;
		this->sec = sec;
	}
};
int sr,sc,er,ec;
int m,n;
char grid[N][N];
int vis[N][N][D][COL];
int bfs() {
	memset(vis,0,sizeof(vis));
	
	queue<Node> q;

	q.push(Node(sr,sc,0,0,0));
	vis[sr][sc][0][0] = true;

	int r,c,dir,col,sec;

	while( !q.empty()) {

		Node front = q.front();
		q.pop();
		
		if(front.r == er && front.c == ec && front.col == 0) {
			return front.sec;
		}
		
		for(int i =0 ; i< 4; i++) {
			if( i == front.dir) {
				r = front.r + dr[i];
				c = front.c + dc[i];
				dir = front.dir;
				col = (front.col + 1) % COL;
				sec = front.sec + 1;
				
				if( r >= 0 && r < m && c >= 0 && c < n && !vis[r][c][dir][col] && grid[r][c] != '#') {
					vis[r][c][dir][col] = true;
					q.push( Node(r,c,dir,col,sec) );
				}
			}else  {
				r = front.r;
				c = front.c;
				dir = front.dir;
				col = front.col;
				sec = front.sec + 1;
				if( (dir + 1) % D == i || (dir + 3) % D == i) {
					if( !vis[r][c][i][col]) {
						vis[r][c][i][col] = true;
						q.push(Node(r,c,i,col,sec));
					}
				}
			}
		}
	}
	return -1;
}
int main() {
	int cas = 0;
	while(scanf("%d%d",&m,&n) != EOF && ( m || n)) {
		for(int i = 0; i < m; i++) {
			scanf("%s",grid[i]);
			for(int j = 0; j < n; j++) {
				if( grid[i][j] == 'S') {
					sr = i;
					sc = j;
				}else if(grid[i][j] == 'T') {
					er = i;
					ec = j;
				}
			}
		}
		if(cas) puts("");
		printf("Case #%d\n",++cas);
		int ans = bfs();
		if(ans == -1) {
			printf("destination not reachable\n");
		}
		else {
			printf("minimum time = %d sec\n",ans);
		}
	}
	return 0;
}

你可能感兴趣的:(uva,the,Monocycle)