当前位置: 代码迷 >> 编程 >> 【CodeCraft赛事】Problem 3——INVESCAPE (迷宫BFS)
  详细解决方案

【CodeCraft赛事】Problem 3——INVESCAPE (迷宫BFS)

热度:2778   发布时间:2013-02-26 00:00:00.0
【CodeCraft比赛】Problem 3——INVESCAPE (迷宫BFS)

CodeCraft是印度IIT大学举办的一个算法邀请赛(这个大学貌似在印度挺出名,有个笑话:印度某集团的老总谈自己的儿子说,我这个不争气的儿子,考IIT都没考上,我气得没办法,只好把他送到了哈佛。。。囧rz)目前只有这一届。

在人人上看到了ACM-ICPC主页君过年期间比赛的新闻,于是很有兴趣便参加了一下,据官网说还会再搞。第一名200美刀,第二名75美刀。我又折腾了一晚上,3AC拿了第30名,小红花一朵大笑

比赛题目链接(注册后才能查看):http://felicity.iiit.ac.in/web2py/Portal/default/event_home?event_id=5&tab_id=102

题目:

Problem Statement

Sue Storm needs to escape from Victor Von Doom's Maze, which is in the form of a NxN grid. At some cells Sonic Detectors have been installed which will alert the enemy. Sue can stay invisible for an indefinite amount of time but she must not come in contact with these Sonic Detectors. Only possible moves are left, right, up or down. Help her find the way to the exit (N,N) where Ben and Reed are waiting for her.
Assume she always starts at (1,1). It is ensured that there will be no detector at (1,1) and (N,N).

INPUT

First line of input contains number of Test Cases T. Each test case begins with N, depicting the size of maze. Then follow N lines. Each of these N lines contains N characters. Jth character in the Ith line is 'D' if there is a Sonic Detector installed or '.' otherwise.

OUTPUT

Print exactly T lines each containing either "YES" if she can reach the exit, "NO" otherwise.

Constraints:

1 <= T <= 100
1 <= N <= 100

Sample Input:

22.D..2.DD.

Output:

YESNO

Limits

Memory Limit : 32MB
Time Limit : 1seconds
Soure Code Limit : 5kb

热身题,挺水的一个BFS,从坐标(1,1)——> (n,n),最终能不能走到,手打1AC。

#include <iostream>#include <queue>#include <cstring>using namespace std;const int INF=105;int mapsize=0;int finder=0;char maze[105][105];int visited[105][105];int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};class point{	public:		int x;		int y;};void Initmaze(int x){	memset(visited,0,sizeof(visited));	for(int i=1;i<=x;i++)	{		for(int j=1;j<=x;j++)		{			cin>>maze[i][j];		}	}}void bfs(){	point start,end;	queue<point> Q;	start.x=1;	start.y=1;	visited[1][1]=1;	Q.push(start);	while(!Q.empty())	{		end=Q.front();		Q.pop();		for(int i=0;i<4;i++)		{			start.x=end.x+dir[i][0];			start.y=end.y+dir[i][1];			while(start.x>=1 && start.x<=mapsize && start.y>=1 && start.y<=mapsize && maze[start.x][start.y]=='.')			{				if(visited[start.x][start.y]==0)				{					if(start.x==mapsize && start.y==mapsize)					{						finder=1;						return;					}					visited[start.x][start.y]=1;					Q.push(start);				}				start.x+=dir[i][0];				start.y+=dir[i][1];			}		}	}	}int main(){	int testcase;	cin>>testcase;	while(testcase--)	{		cin>>mapsize;		Initmaze(mapsize);		finder=0;		if(mapsize==1)		{			cout<<"YES"<<endl;			continue;		}		else		{			bfs();			if(finder==1)				cout<<"YES"<<endl;			else				cout<<"NO"<<endl;		}	}	return 0;}



  相关解决方案