poj 3083 Children of the Candy Corn(广度搜索)

                             Children of the Candy Corn

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 10997 Accepted: 4736

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 为地图的终点,求:

靠着左边的墙走,求走的步数,即输出的第一个数

然后靠着右边的墙走,求走的步数,即输出的第二个数

其实也挺简单的,就是在控制行走方向上有点难度,基本上用了DFS 和 BFS 搜索

第三个数就是最短路了


#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>

using namespace std;

struct node
{
	int x,y;
	int step;
};
char Map[45][45];
bool vis[45][45];
int m,n;
int px,py;
int Step_Left,Step_Right,Step_min;
int Dir[][2] = {{0,1},{1,0},{0,-1},{-1,0}};

bool Judge(int x,int y)
{
	if(x >= 0 && x < n && y >= 0 && y < m && Map[x][y] != '#')
		return true;
	return false;
}

void DFS_Left(int step,int dir,int x,int y)
{
	if(Map[x][y] == 'E')
	{
		Step_Left = step;
		return ;
	}
	dir--;
	dir = (dir + 4) % 4;
	for(int i=0;i<4;i++)
	{
		int d = (dir + i + 4) % 4;
		int fx = x + Dir[d][0];
		int fy = y + Dir[d][1];
		if(Judge(fx,fy))
		{
			DFS_Left(step+1,d,fx,fy);
			break;
		}
	}
}

void DFS_Right(int step,int dir,int x,int y)
{
	if(Map[x][y] == 'E')
	{
		Step_Right = step;
		return ;
	}
	dir++;
	dir %= 4;
	for(int i=0;i<4;i++)
	{
		int d = (dir - i + 4) % 4;
		int fx = x + Dir[d][0];
		int fy = y + Dir[d][1];
		if(Judge(fx,fy))
		{
			DFS_Right(step+1,d,fx,fy);
			break;
		}
	}
}

void BFS()
{
	memset(vis,false,sizeof(vis));
	queue<node >que;
	node a,b;
	a.x = px;
	a.y = py;
	a.step = 1;
	vis[a.x][a.y] = true;
	que.push(a);
	while(!que.empty())
	{
		a = que.front();
		que.pop();
		if(Map[a.x][a.y] == 'E')
		{
			Step_min = a.step;
			return ;
		}
		for(int i=0;i<4;i++)
		{
			b.x = a.x + Dir[i][0];
			b.y = a.y + Dir[i][1];
			b.step = a.step + 1;
			if(Judge(b.x,b.y) && !vis[b.x][b.y])
			{
				vis[b.x][b.y] = true;
				que.push(b);
			}
		}
	}
}

int main()
{
	//freopen("in.txt","r",stdin);
	int T;
	scanf("%d",&T);
	while(T--)
	{
		memset(Map,0,sizeof(Map));
		scanf("%d%d",&m,&n);
		for(int i=0;i<n;i++)
		{
			scanf("%s",Map[i]);
			for(int j=0;j<m;j++)
			{
				if(Map[i][j] == 'S')
				{
					px = i;
					py = j;
				}
			}
		}
		if(px == 0)
		{
			DFS_Left(1,2,px,py);
			DFS_Right(1,2,px,py);
		}
		else if(px == n-1)
		{
			DFS_Left(1,0,px,py);
			DFS_Right(1,0,px,py);
		}
		else if(py == 0)
		{
			DFS_Left(1,1,px,py);
			DFS_Right(1,1,px,py);
		}
		else if(py == m-1)
		{
			DFS_Left(1,3,px,py);
			DFS_Right(1,3,px,py);
		}
		BFS();
		printf("%d %d %d\n",Step_Left,Step_Right,Step_min);
	}
	return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值