1. 程式人生 > >模擬+bfs(poj3083 children of the candy corn)

模擬+bfs(poj3083 children of the candy corn)

Children of the Candy Corn
Time Limit: 1000MSMemory Limit: 65536K
Total Submissions: 14932Accepted: 6435

Description

The cornfield maze is a popular Halloween treat. Visitors are shown the entrance and must wander through the maze facing zombies, chainsaw-wielding psychopaths, hippies, and other terrors on their quest to find the exit. 

One popular maze-walking strategy guarantees that the visitor will eventually find the exit. Simply choose either the right or left wall, and follow it. Of course, there's no guarantee which strategy (left or right) will be better, and the path taken is seldom the most efficient. (It also doesn't work on mazes with exits that are not on the edge; those types of mazes are not represented in this problem.) 

As the proprieter of a cornfield that is about to be converted into a maze, you'd like to have a computer program that can determine the left and right-hand paths along with the shortest path so that you can figure out which layout has the best chance of confounding visitors.

Input

Input to this problem will begin with a line containing a single integer n indicating the number of mazes. Each maze will consist of one line with a width, w, and height, h (3 <= w, h <= 40), followed by h lines of w characters each that represent the maze layout. Walls are represented by hash marks ('#'), empty space by periods ('.'), the start by an 'S' and the exit by an 'E'. 

Exactly one 'S' and one 'E' will be present in the maze, and they will always be located along one of the maze edges and never in a corner. The maze will be fully enclosed by walls ('#'), with the only openings being the 'S' and 'E'. The 'S' and 'E' will also be separated by at least one wall ('#'). 

You may assume that the maze exit is always reachable from the start point.

Output

For each maze in the input, output on a single line the number of (not necessarily unique) squares that a person would visit (including the 'S' and 'E') for (in order) the left, right, and shortest paths, separated by a single space each. Movement from one square to another is only allowed in the horizontal or vertical direction; movement along the diagonals is not allowed.

Sample Input

2
8 8
########
#......#
#.####.#
#.####.#
#.####.#
#.####.#
#...#..#
#S#E####
9 5
#########
#.#.#.#.#
S.......E
#.#.#.#.#
#########

Sample Output

37 5 5
17 17 9

題目大意:

給定一個迷宮,S是起點,E是終點,#是牆不可走,.可以走

先輸出左轉優先時,從S到E的步數

再輸出右轉優先時,從S到E的步數

最後輸出S到E的最短步數

W為寬,列數

H為高,行數

本題要求輸出從S到E的最短路,沿左邊牆和沿右邊牆走的步數。求S到E的最短路用廣搜,沿牆走用模擬。

以沿左邊牆走為例,介紹下模擬的思路:

1、首先設選擇方向ldx[4]為左上右下.light為左方,right為右方,front為前方

2、從起點出發,找到其相鄰的牆,左沿牆能走通,由牆所在的方向為左確定前方,並前進一步。lsteps++;

3、根據前方確定左方和右方。

4、判斷如左邊不為牆且不越界,將原左方作為前方,即左轉,front=light;

5、如果前方能走通,前進一步。lsteps++;

6、如果左邊和前方都不通,將原右方作為前方,即右轉,front=right;

7、重複3,4,5,6,知道找到出口。

左轉、右轉優先搜尋時必須標記當前位置時的方向


最初的方向由起點S確定,而下一步的方向則由前一步的走向決定

這裡有一點必須要注意的:

左邊、右邊優先搜尋都不是找最短路,因此走過的路可以再走,無需標記走過的格


#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
char map[50][50];
int n,w,h,sx,sy,ex,ey,lsteps,rsteps;
bool flag,vis[50][50];
int dx[4]={0,0,1,-1}; int dy[4]={1,-1,0,0};
int ldx[4]={0,-1,0,1}; int ldy[4]={-1,0,1,0}; //左上右下 
int rdx[4]={0,1,0,-1}; int rdy[4]={1,0,-1,0}; //右下左上 
struct node
{
	int x,y;
	int steps;
};
int bfs()
{
	memset(vis,false,sizeof(vis));
	queue<node> q;
	node cur;
	cur.x=sx; cur.y=sy; cur.steps=1;
	vis[sx][sy]=true;
	q.push(cur);
	while(!q.empty())
	{
		node cur=q.front();
		q.pop();
		if(cur.x==ex&&cur.y==ey) return cur.steps;//找到出口 
		for(int i=0;i<4;i++)
		{
			node next;
			next.x=cur.x+dx[i];
			next.y=cur.y+dy[i];
			next.steps=cur.steps+1;
			if(next.x>=0&&next.x<h&&next.y>=0&&next.y<w&&map[next.x][next.y]!='#'&&!vis[next.x][next.y])
			{
				vis[next.x][next.y]=true;
				q.push(next);
			}
		}
	}
	return 0;
}
void left()//沿左邊牆走 
{
	int i,px,py,front;//front為前方 
	for(i=0;i<4;i++)
	{
		px=sx+ldx[i];
		py=sy+ldy[i];
		if(px>=0&&px<h&&py>=0&&py<w&&map[px][py]=='#')//找相鄰的牆 
		{
			front=(i+1)%4;
			px=sx+ldx[front];
			py=sy+ldy[front];
			if(px>=0&&px<h&&py>=0&&py<w&&map[px][py]!='#') break;//左邊沿著他走有路 
		}
	}
	lsteps++;//向前一步 
	while(px!=ex||py!=ey)//找到終點退出 
	{
		int light=(front+3)%4;//左方 
		int right=(front+1)%4;//右方 
		int back=(front+2)%4;
		if(px+ldx[light]>=0&&px+ldx[light]<h&&py+ldy[light]>=0&&py+ldy[light]<w&&map[px+ldx[light]][py+ldy[light]]!='#') 
		    front=light;//如果左邊不越界且不是牆,左轉 
	    else if(px+ldx[front]<0||px+ldx[front]>=h||py+ldy[front]<0||py+ldy[front]>=w||map[px+ldx[front]][py+ldy[front]]=='#') 
		    front=right;
	    if(px+ldx[front]>=0&&px+ldx[front]<h&&py+ldy[front]>=0&&py+ldy[front]<w&&map[px+ldx[front]][py+ldy[front]]!='#')
	    {
	    	px+=ldx[front];
	    	py+=ldy[front];
	    	lsteps++;//前進一步 
		}
	}
	return ;
}
void right()
{
	int i,px,py,front;//前方 
	for(i=0;i<4;i++)//找到可走的路並確定前方 
	{
		px=sx+rdx[i];
		py=sy+rdy[i];
		if(px>=0&&px<h&&py>=0&&py<w&&map[px][py]=='#')//找相鄰的牆 
		{
			front=(i+3)%4;
			px=sx+ldx[front];
			py=sy+ldy[front];
			if(px>=0&&px<h&&py>=0&&py<w&&map[px][py]!='#')
			    break;//右邊沿著他走前方有路 
		}
	}
	rsteps++;
	while(px!=ex||py!=ey)
	{
		int light=(front+3)%4;
		int right=(front+1)%4;
		int back=(front+2)%4;
		if(px+ldx[right]>=0&&px+ldx[right]<h&&py+ldy[right]>=0&&py+ldy[right]<w&&map[px+ldx[right]][py+ldy[right]]!='#') 
		    front=right;//如果右邊不越界且不是牆,右轉 
	    else if(px+ldx[front]<0||px+ldx[front]>=h||py+ldy[front]<0||py+ldy[front]>=w||map[px+ldx[front]][py+ldy[front]]=='#') 
		    front=light;
	    if(px+ldx[front]>=0&&px+ldx[front]<h&&py+ldy[front]>=0&&py+ldy[front]<w&&map[px+ldx[front]][py+ldy[front]]!='#')
	    {
	    	px+=ldx[front];
	    	py+=ldy[front];
	    	rsteps++;
		}
	}
	return ;
}
int main()
{
	cin>>n;
	while(n--)
	{
		cin>>w>>h;
		for(int i=0;i<h;i++)
		{
		    for(int j=0;j<w;j++)
			{
				cin>>map[i][j];
				if(map[i][j]=='S') sx=i,sy=j;
				else if(map[i][j]=='E') ex=i,ey=j;
			}
		}
		lsteps=rsteps=1;
		right();left();
		cout<<lsteps<<" "<<rsteps<<" "<<bfs()<<endl;
	}
	return 0;
 }