您的位置:首页 > 大数据 > 人工智能

HDU3912 Turn Right 模拟 2011 Multi-University Training Contest 8 - Host by HUST

2011-08-06 20:04 459 查看

Turn Right

Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 504 Accepted Submission(s): 173



Problem Description
This summer, ELT and his classmates went to Beijing for a training of coding. ELT have never been to Beijing before, so at the weekend, he together with some friends went to the National Museum, it's free for students!

The National Museum consists of many parts. One part of it is an exhibition of Ancient China From Xia Dynasty to Qing Dynasty, it needs a big room to show all the things. What's more, there exist many walls to hang pictures. The boundary of this room is walls
except the entrance and exit.

With walls, an entrance and an exit, this room can be regarded as a maze. To make it simple, this room is a R*C grid, wall is constructed at some edges of grid. The entrance is always at the first row, and the exit is always at the last row, just like the picture
below.



ELT can't remember his direction in maze, but he is a clever boy. He knew an algorithm called "Always Turn Right", it's procedure is as follows: at any grid of this room, if we can turn right(no wall at right side), then we must turn right; if we can't turn
right but can go straight forward, then we must go forward; if we can't go forward but can turn left, then we must turn left; if we can't even turn left, we just turn backward. In the picture above, if we use this algorithm, we'll visit these grids in order:
Entrance --> (0, 1) --> (0, 0) --> (0, 1) --> (0, 2) --> (1, 2) --> (1, 1) --> (1, 0) --> (2, 0) --> (1, 0) --> (1, 1) --> (2, 1) --> (2, 2) --> Exit. Very easy, doesn't it?

ELT uses "Always Turn Right" algorithm to visit this room from entrance to exit, and then from exit to entrance. He wants to know whether he walked all grids in the room. Now ELT is dizzy because the maze is too big, can you help him?

Input
First line is an integer T, means T test cases. In each test case, the first line has four numbers: R, C, Ent_Column, Exit_Column. Ent_Column is the column number of entrance; Exit_Column is the column number of exit.

Then following 2*R-1 lines, 2*i line have C-1 numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i, j+1), 2*i+1 line have C numbers, the j-th number shows whether there is a wall between grid(i, j) and grid(i+1, j). Number 1
represents a wall, 0 represents no wall.

We guarantee that there exists a path from entrance to exit.

2 <= R, C <= 500

0 <= Ent_Column, Exit_Column < C

Output
If ELT can walk all grids in the room, print one line "YES", otherwise, print one line "NO".

Sample Input
1
3 4 1 2
0 0 0
1 1 0 1
0 0 0
0 0 0 0
1 0 0


Sample Output
YES


Source
2011 Multi-University Training Contest 8 - Host by HUST

Recommend
lcy

简单的模拟题,却杯具了很久,所以记录下来作为警戒。
刚开始写成递归爆栈了。。。
后面WA了几次,因为刚开始写的走到有出口的格子就退出是错误的,还要方向正确才行。

#include<cstdio>
#include<cstring>

int t,n,m,dir[4][2]={1,0,0,-1,-1,0,0,1};
bool f[505][505],b[505][505],a[505][505][4];
void dfs(int x,int y,int d)
{
	int i,k,xx,yy;
	while(1)
	{
		f[x][y]=1;
		if(x<1||y<1||x>n||y>m)
			break;
		for(i=1;i>=-2;i--)
		{
			k=(d+i+4)%4;
			if(a[x][y][k])
				continue;
			xx=x+dir[k][0];
			yy=y+dir[k][1];
			if(xx>=0&&yy>=0&&xx<=n+1&&yy<=m+1)
			{
				x=xx;
				y=yy;
				d=k;
				break;
			}
		}
	}
}
int ok()
{
	int i,j;
	for(i=1;i<=n;i++)
		for(j=1;j<=m;j++)
			if(!f[i][j])
				return 0;
	return 1;
}
int main()
{
	scanf("%d",&t);
	while(t--)
	{
		int i,j,s,e;
		memset(f,0,sizeof(f));
		memset(b,1,sizeof(b));
		scanf("%d%d%d%d",&n,&m,&s,&e);
		s++;e++;
		for(i=0;i<2*n-1;i++)
			if(i%2==0)
			{
				for(j=1;j<m;j++)
				{
					scanf("%d",&b[i/2+1][j]);
					a[i/2+1][j][3]=b[i/2+1][j];
					a[i/2+1][j][1]=b[i/2+1][j-1];
				}
				a[i/2+1][m][3]=1;
				a[i/2+1][m][1]=b[i/2+1][m-1];
			}
			else
			{
				for(j=1;j<=m;j++)
				{
					scanf("%d",&b[i/2+1][j]);
					a[i/2+1][j][0]=b[i/2+1][j];
					a[i/2+1][j][2]=b[i/2][j];
				}
			}
		for(j=1;j<=m;j++)
		{
			a
[j][2]=b[n-1][j];
			a
[j][0]=1;
		}
		a[1][s][2]=0;
		a
[e][0]=0;
		dfs(1,s,0);
		dfs(n,e,2);
		puts(ok()?"YES":"NO");
	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐