您的位置:首页 > 编程语言 > Go语言

Google中国2014校园招聘笔试Round A China New Grad Test Problem D. Cross the maze

2013-09-25 13:48 465 查看

Problem

Edison, a robot, does not have a right hand or eyes. As a brave robot, he always puts his left hand on the wall no matter he walks or turns around. Because he thinks it is too dangerous, Edison does not walk backward.

Assume that Edison has found himself in a square-shaped maze of NxN square cells which is surrounded by walls from the outside. In the maze, some of the cells are also walls. Edison can only move
between two empty cells in four directions, north, south, west and east. In order to get out of the maze, he drafts a plan. He uses his left hand to lean on the wall and goes by following the wall.

Here is the question, is Edison able to get out of the maze in at most 10,000 steps? If he can make it, output the path. By getting out of the maze, he only needs to be in the exit cell. If the starting cell is the same as the exit, Edison won't need to
move and can directly get out of the maze.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case starts with an integer N. N is
the size of the maze. The following N lines, each line contains N characters which may be '.' or '#'. '.' is an empty cell, '#' is a wall. Followed by a line which contains
four integers: sx, sy, ex, ey. (sx, sy) means that Edison is standing on row sx and
column sy as his starting cell, (ex, ey) is the exit of the maze. (sx, sy) is guaranteed to be at one of the 4 corners
of the maze, and Edison can only touch the wall on 4 adjacent cells(not 8) initially. (ex, ey) can be anywhere in the maze. Note that the top-left corner is at position (1,1).

Output

For each test case, output a line containing "Case #x: y", where x is the case number (starting from 1) and y is "Edison ran out of energy." (without the quotes) if Edison can't reach the exit of the maze in at most 10,000 steps, otherwise y should be the
number of steps followed by another line which contains y characters to describe the path (each character should be E for east, S for south, W for west or N for north). There is no character to represent the turning around. We don't care about the turning
around steps, please only output the path of how Edison will cross the maze.

Limits

1 ≤ T ≤ 30.

1 ≤ sx, sy, ex, eyN.

The starting cell and the exit of the maze will always be an empty cell. And the starting cell and the exit of the maze won't be the same.

Small dataset

2 ≤ N ≤ 10.

Large dataset

2 ≤ N ≤ 100.

Sample

Input

Output

3
2
.#
#.
1 1 2 2
5
.##.#
.....
...#.
.###.
...#.
1 1 5 3
3
...
.#.
...
1 1 3 3

Case #1: Edison ran out of energy.
Case #2: 22
SEEENSESSSNNNWWSWWSSEE
Case #3: 4
EESS

Note:

In the 2nd test case after moving 1 cell down from his starting cell, Edison will still be able to lean on the wall at the cell (1,2) by his left hand.

In the third test case, due to Edison can't touch the wall at cell (2,2) initially, so he has to go east in his first step.



类型:图论 难度:2.5

题意:一个看不见的机器人,只有左手,把它放在一个N*N迷宫的四个角落之一,它只能左手扶墙贴墙摸索着走,只能走东南西北四个方向,给定起点(保证是四个角落之一)终点,求机器人是否能走到终点,若走到的话,给出步数和每一步所走的方向。

分析:几个问题需要注意:

1、因为是只用左手扶墙走,所以机器人若走到一个点左边没有墙,一定会左转再直走;

2、若走到一个点前面是墙,则一定会顺时针旋转,依次摸索每个方向是否能走;

3、因为是贴墙走,只有一条路一种走法,最后会回到起点,且回到起点时的方向会是初始方向。



所以,只需要按照规则依次走完并记录每次的方向即可,遇到终点即返回;若走完依次未遇到终点则返回失败。

递归求解,fun(sx,sy,ex,ey,head),参数为起点,终点,当前方向

细节:

1、我采用递归求解,边界问题很重要,若起点终点相同,直接返回0,若起点本身是墙,直接返回-1。

2、考虑上述分析第1条,每次都先将当前方向head向左转,再一次遍历四个方向

3、用flag[x][y][4]记录每个点的4个方向是否遍历过,若之前遍历过则跳过这个方向

4、遍历四个方向时,若发现当前方向不是遍历的第一个方向,且左手方向不是墙,证明已经旋转的脱离墙了,那么这个点已经遍历结束,直接跳出循环。(因为默认遍历的第一个方向是先向左转,此时左手方向是上一个走过的点,一定不是墙,但是继续顺时针转的话,必须保证左手是墙才能前进)

5、每次记录当前走的方向,并在递归失败后去掉。这里用string记录的时候,记录到4500个就崩溃了。。不知为何。。后来用char数组就没问题了

代码:

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<string>
#include<iostream>
#define MIN(x,y) (x)<(y)?(x):(y)
using namespace std;

const int N=110;
char m

;
bool mz

;
int dir[4][2] = {{0,-1},{-1,0},{0,1},{1,0}};
char dirn[4] = {'W','N','E','S'};
bool flag

[4];
char step[20010];
int stepnum;

int fun(int sx,int sy,int ex,int ey,int head)
{
	//printf("sx %d sy %d step %d\n",sx,sy,step);
	if(sx==ex && sy==ey) return 0;
	if(mz[sx][sy] == 0) return -1;
	
	head = (head+3)%4;
	
	for(int i=0; i<4; i++)
	{		
		int headx = sx+dir[head][0];
		int heady = sy+dir[head][1];
		int left = (head+3)%4;
		int leftx = sx+dir[left][0];
		int lefty = sy+dir[left][1];
	
		if(flag[sx][sy][head]) continue;
		if(i>0 && mz[leftx][lefty]) break;
		
		step[stepnum++] = dirn[head];
		flag[sx][sy][head] = 1;
			
		int tmp = fun(headx,heady,ex,ey,head);
		if(tmp >= 0) return tmp + 1;
		
		step[--stepnum] = 0;
		
		head = (head+1)%4;
	}
	
	return -1;
}

int main()
{
	freopen("D-large-practice.in","r",stdin);
	freopen("D-large-practice.out","w",stdout);
	
	int t;
	scanf("%d",&t);
	for(int cnt=1;cnt<=t;cnt++)
	{
		int n;
		scanf("%d",&n);
		
		memset(m,0,sizeof(m));
		memset(mz,0,sizeof(mz));
		memset(flag,0,sizeof(flag));
		
		for(int i=0; i<n; i++)
			scanf("%s",m[i]);
		for(int i=0; i<n; i++)
			for(int j=0; j<n; j++)
				if(m[i][j] == '.')
					mz[i+1][j+1] = 1;
			
		int sx,sy,ex,ey;
		scanf("%d%d%d%d",&sx,&sy,&ex,&ey);
		
		memset(step,0,sizeof(step));
		stepnum = 0;
		
		int head;
		
		if(sx==1 && sy==1) head = 3;
		else if(sx==1 && sy>1) head = 0;
		else if(sx>1 && sy==1) head = 2;
		else head = 1;
		
		int ans = fun(sx,sy,ex,ey,head);
		
		printf("Case #%d: ",cnt);
		if(ans < 0) printf("Edison ran out of energy.\n"); 
		else if(ans == 0) printf("0\n");
		else printf("%d\n%s\n",ans,step);
	} 
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: