POJ-3083 Children of the Candy Corn(深搜,广搜)

Children of the Candy Corn
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 8505 Accepted: 3728

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

思路:

        题不难,但同时考到了深搜+广搜;

        需要注意,往左优先的深搜和往右优先的深搜搜出来的最小步数不一定是整体的最小步数;求最小步数,必须广搜!


代码:

#include <stdio.h>
#include <string.h>
#include <queue>
#define N 45

using namespace std;

struct Node{
	int x;
	int y;
	int s;
	int fx;
};

char map[N][N];
bool bz[N][N];
int m, n;
queue<Node>q;
int fx[4][2] = {{-1, 0}, {1, 0}, {0, 1}, {0, -1}};
int f[4][4][2] = {
	{{-1, 0}, {1, 0}, {0, 1}, {0, -1}},
	{{0, 1}, {0, -1}, {1, 0}, {-1, 0}},
	{{1, 0}, {-1, 0}, {0, -1}, {0, 1}},
	{{0, -1}, {0, 1}, {-1, 0}, {1, 0}},
};

int bfs();
int dfs(Node start, int fxi);
void getfx(Node &cur);

int main()
{
	int k;
	scanf("%d", &k);
	while(k --){
		while(!q.empty()) q.pop();
		memset(map, '#', sizeof(map));
		memset(bz, 0, sizeof(bz));
		scanf("%d%d", &m, &n);
		Node start;
		for(int i = 1; i <= n; i ++){
			for(int j = 1; j <= m; j ++){
				scanf(" %c", &map[i][j]);
				if(map[i][j] == 'S'){
					start.x = i;
					start.y = j;
					start.s = 1;	
					q.push(start);
					map[i][j] = '#';
				}
			}
		}
		getfx(start);
		int minl = dfs(start, 0);
		int minr = dfs(start, 1);
		int mins = bfs();
		printf("%d %d %d\n", minl, minr, mins);
	}

	return 0;
}

int bfs()
{
	Node cur;
	while(1){
		cur.x = q.front().x;
		cur.y = q.front().y;
		cur.s = q.front().s + 1;
		q.pop();

		for(int i = 0; i < 4; i ++){
			Node nw = cur;
			nw.x += fx[i][0];
			nw.y += fx[i][1];
			if(map[nw.x][nw.y] == '#' || bz[nw.x][nw.y]) continue;
			if(map[nw.x][nw.y] == 'E') return nw.s;
			bz[nw.x][nw.y] = 1;
			q.push(nw);
		}

		if(q.empty()) return -1;
	}
}

int dfs(Node cur, int fxi)
{
	Node nw;
	int i = cur.fx;
	int ok;
	int x = cur.x + f[i][fxi][0];
	int y = cur.y + f[i][fxi][1];
	char c = map[x][y];
	if(c == 'E'){				// fxi 方向
		return cur.s + 1;
	}
	else if(c == '.'){
		nw.x = x;
		nw.y = y;
		nw.s = cur.s + 1;
		if(fxi == 0) nw.fx = (cur.fx + 3) % 4;
		else nw.fx = (cur.fx + 1) % 4;
		ok = dfs(nw, fxi);
		if(ok) return ok;
	}
	else{
		x = cur.x + f[i][2][0];
		y = cur.y + f[i][2][1];
		c = map[x][y];
		if(c == 'E'){		// cur.fx 方向
			return cur.s + 1;
		}
		else if(c == '.'){
			nw.x = x;
			nw.y = y;
			nw.s = cur.s + 1;
			nw.fx = cur.fx;
			ok = dfs(nw, fxi);
			if(ok) return ok;
		}
		else{
			int nfx = (fxi + 1) % 2;
			x = cur.x + f[i][nfx][0];
			y = cur.y + f[i][nfx][1];
			c = map[x][y];
			if(c == 'E'){		// - fxi 方向
				return cur.s + 1;
			}
			else if(c == '.'){
				nw.x = x;
				nw.y = y;
				nw.s = cur.s + 1;
				if(nfx == 0) nw.fx = (cur.fx + 3) % 4;
				else nw.fx = (cur.fx + 1) % 4;
				ok = dfs(nw, fxi);
				if(ok) return ok;
			}
			else{				// -cur.fx 方向
				nw.x = cur.x + f[i][3][0];
				nw.y = cur.y + f[i][3][1];
				nw.s = cur.s + 1;
				nw.fx = (cur.fx + 2) % 4;
				ok = dfs(nw, fxi);
				if(ok) return ok;
			}
		}
	}

	return 0;
}

void getfx(Node &cur)
{
	if(cur.x == 1) cur.fx = 1;
	if(cur.x == n) cur.fx = 3;
	if(cur.y == 1) cur.fx = 0;
	if(cur.y == n) cur.fx = 2;
}

	

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值