浙江财经大学
信息管理与工程学院

POJ 3083 Children of the Candy Corn

本文由 Ocrosoft 于 2016-08-25 21:12:17 发表

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

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

Solution

题意:一个保证能走到出口的迷宫,从入口开始,扶着左边的墙走要多少步?扶着右边的墙走要多少步?
不做要求最少要多少步?

思路:最短路径BFS就可以求出来,扶着墙走...
假设要求扶着左边的墙走,如果现在在往上走,那么优先选择往左走,然后是上,再是右,最后是往下走。
为什么要这么走?如果我们先选择往上走,如果左边有路,那么就不满足扶着左边墙走的规则了。其他也一样。 ↑:← ↑ → ↓

→:↑ → ↓ ← 

↓:→ ↓ ← ↑ 

←:↓ ← ↑ →   
假设以← ↑ → ↓保存以上内容,那么(dire - 1 + 4) % 4就是每组的第一个方向,((dire - 1 + 4) % 4 + i) % 4
就能走完四个方向。左上右下分别为0 1 2 3。
#include <set>
#include <map>
#include <list>
#include <cmath>
#include <stack>
#include <queue>
#include <ctime>
#include <string>
#include <cstdio>
#include <vector>
#include <cctype>
#include <climits>
#include <sstream>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <functional>
#define ms(a) memset(a,0,sizeof(a))
typedef long long ll;
const int INF = INT_MAX;
const int MAXN = 100 + 10;
using namespace std;
int n, m;
char mp[MAXN][MAXN];
int sx, sy, ex, ey;
int dx[] = { 0, -1, 0, 1 };
int dy[] = { -1, 0, 1, 0 };
int dleft[][2] = { 0, -1 , -1, 0 , 0, 1 , 1, 0 };
int dright[][2] = { 0, 1 , -1, 0 , 0, -1 , 1, 0 };
int dfs(int x, int y, int dire, int step, int dir[][2])
{
	for (int i = 0; i < 4; i++)
	{
		int j = ((dire - 1 + 4) % 4 + i) % 4;
		int nx, ny;
		nx = x + dir[j][0];
		ny = y + dir[j][1];
		if (nx == ex&&ny == ey)return step + 1;
		if (nx<0 || nx>n || ny<0 || ny>m)continue;
		if (mp[nx][ny] == '#')continue;
		return dfs(nx, ny, j, step + 1, dir);
	}
	return -1;
}
struct P
{
	int x, y, step;
}p;
int bfs(int sx, int sy)
{
	bool vis[MAXN][MAXN] = { 0 };
	queue<P> q;
	p.x = sx, p.y = sy, p.step = 1;
	q.push(p);
	vis[sx][sy] = 1;
	while (!q.empty())
	{
		p = q.front(); q.pop();
		if (p.x == ex&&p.y == ey)return p.step;
		P pp;
		for (int i = 0; i < 4; i++)
		{
			pp.x = p.x + dx[i];
			pp.y = p.y + dy[i];
			pp.step = p.step + 1;
			if (pp.x<0 || pp.x>n || pp.y<0 || pp.y>m)continue;
			if (vis[pp.x][pp.y])continue;
			if (mp[pp.x][pp.y] != '#')
			{
				vis[pp.x][pp.y] = 1;
				q.push(pp);
			}
		}
	}
	return -1;
}
int main()
{
	int T;
	cin >> T;
	while (T--)
	{
		cin >> m >> n;
		for (int i = 0; i < n; i++)cin >> 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;
			}
		}
		int a = 0, b = 0;
		if (!sx)a = 3, b = 3;
		else if (sx == n - 1)a = 1, b = 1;
		else if (!sy)a = 2, b = 0;
		else if (sy == m - 1)a = 0, b = 2;

		printf("%d ", dfs(sx, sy, a, 1, dleft));
		printf("%d ", dfs(sx, sy, b, 1, dright));
		printf("%d\n", bfs(sx, sy));
	}
	return 0;
}


欢迎分享与转载,请保留链接与出处。Ocrosoft » POJ 3083 Children of the Candy Corn

点赞 (0)or拍砖 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址