POJ3083 Children of the Candy Corn(BFS+DFS)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/UncleJokerly/article/details/83865840

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

POJ3083

题意:

先沿着左边的墙从 S 一直走,求到达 E 的步数。

再沿着右边的墙从 S 一直走,求到达 E 的步数。

最后求最短路。

解题思路:

最短路好办,关键是沿着墙走不太好想。

但只要弄懂如何转,这题就容易了。

单就沿着左走看一下:

当前方向     检索顺序

     ↑ :      ← ↑ → ↓

    → :        ↑ → ↓ ← 

     ↓ :      → ↓ ← ↑ 

    ← :        ↓ ← ↑ → 

如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。

在DFS时,加一个参数,用来保存当前的方向。

AC代码:

#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;

char mp[110][110];
int sx,sy,ex,ey,n,m,book[110][110];

int nx[4][2]={0,-1,-1,0,0,1,1,0};//bfs
int nxl[4][2]={0,-1,-1,0,0,1,1,0};//ldfs
int nxr[4][2]={0,1,-1,0,0,-1,1,0};//rdfs

struct node
{
	int x,y,step;
};

node getnode(int x,int y,int step)
{
	node q;
	q.x=x;
	q.y=y;
	q.step=step;
	return q;
}

int dfs(int x,int y,int d,int step,int dir[][2])
{
	for(int i=0;i<4;i++)
	{
		int j=((d-1+4)%4+i)%4;
		int tx=x+dir[j][0];
		int ty=y+dir[j][1];
		if(tx>=0&&tx<n&&ty>=0&&ty<m&&mp[tx][ty]!='#')
		{
			if(tx==ex&&ty==ey) return step+1;
			return dfs(tx,ty,j,step+1,dir);
		}
	}
}

int bfs(int sx,int sy)
{
	queue<node> q;
	q.push(getnode(sx,sy,1));
	while(!q.empty())
	{
		for(int i=0;i<4;i++)
		{
			int tx=q.front().x+nx[i][0];
			int ty=q.front().y+nx[i][1];
			if(tx>=0&&tx<n&&ty>=0&&ty<m&&book[tx][ty]==0&&mp[tx][ty]!='#')
			{
				book[tx][ty]=1;
				if(tx==ex&&ty==ey) return q.front().step+1;
				q.push(getnode(tx,ty,q.front().step+1));
			}
		}
		q.pop();
	}
	return -1;
}

int main()
{
	int t,d1,d2;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&m,&n);
		for(int i=0;i<n;i++)
			scanf("%s",mp[i]);
			
		for(int i=0;i<n;i++)
		{
			for(int j=0;j<m;j++)
			{
				if(mp[i][j]=='S') {sx=i;sy=j;}
				else if(mp[i][j]=='E') 	{ex=i;ey=j;}
			}
		}
		
		if(sx==0) {d1=3;d2=3;}
		else if(sx==n-1) {d1=1;d2=1;}
		else if(sy==0) {d1=2;d2=0;}
		else if(sy==m-1) {d1=0;d2=2;}
		
		memset(book,0,sizeof(book));
		book[sx][sy]=1;
		
		printf("%d ",dfs(sx,sy,d1,1,nxl));
		printf("%d ",dfs(sx,sy,d2,1,nxr));
		printf("%d\n",bfs(sx,sy));
	}
	return 0;//
}

猜你喜欢

转载自blog.csdn.net/UncleJokerly/article/details/83865840