您的位置:首页 > 其它

zcmu1032

2018-03-05 08:20 141 查看

1032: Children of the Candy Corn

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 98  Solved: 71
[Submit][Status][Web Board]

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
4000
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

28 8#########......##.####.##.####.##.####.##.####.##...#..##S#E####9 5##########.#.#.#.#S.......E#.#.#.#.##########

Sample Output

37 5 517 17 9

HINT

Source

浙江中医药大学第六届程序设计竞赛
左路径,意思是左优先的走,且左走不了则按顺时针改变方向的走,右路径相反,右优先,按逆时针改变方向走。#include <iostream>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <cmath>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define pi acos(-1)
char s[45][45];
bool flag[45][45][4], f;
int cx[] = { -1,0,1,0 };
int cy[] = { 0,1,0,-1 };
int cdir[][4] = { {3,0,1,2},{1,2,3,0} };
int n, m, sx, sy, ex, ey;
struct road {
int x, y, dir, sum;
};
int bfs() {
bool flag[45][45];
int sum = 1;
memset(flag, false, sizeof(flag));
queue<road> q;
q.push({sx,sy,0,1});
while (!q.empty()) {
road t = q.front();
q.pop();
for (int i = 0; i < 4; ++i) {
road t0;
t0.x = t.x + cx[i];
t0.y = t.y + cy[i];
t0.sum = t.sum + 1;
if (t0.x < 0 || t0.y < 0 || t0.x >= n || t0.y >= m || s[t0.x][t0.y] == '#' || flag[t0.x][t0.y]) {
continue;
}
flag[t0.x][t0.y] = true;
if (t0.x == ex && t0.y == ey) {
return t0.sum;
}
q.push(t0);
}
}
return 0;
}
bool dfs(road t,int c,int& sum) {
//printf("%d %d\n", t.x, t.y);
if (t.x == ex && t.y == ey) {
return true;
}
for (int i = 0; i < 4; ++i) {
road t0;
int dir;
if (c == 0) {
dir = (t.dir + i) % 4;
}
else {
dir = (t.dir - i);
if (t.dir < 0) {
dir = dir + 4;
}
}
t0.x = t.x + cx[dir];
t0.y = t.y + cy[dir];
t0.dir = cdir[c][dir];
if (t0.x < 0 || t0.y < 0 || t0.x >= n || t0.y >= m || s[t0.x][t0.y] == '#' || flag[t0.x][t0.y][t0.dir]) {
continue;
}
flag[t0.x][t0.y][t0.dir] = true;
sum++;
if (dfs(t0,c,sum)) {
return true;
}
}
return false;
}
int main(){
int t;
scanf("%d", &t);
while (t--) {
scanf("%d%d", &m, &n);
for (int i = 0; i < n; ++i) {
scanf("%s", s[i]);
for (int j = 0; j < m; ++j) {
if (s[i][j] == 'S') {
sx = i, sy = j;
s[i][j] = '.';
}
else if (s[i][j] == 'E') {
ex = i, ey = j;
s[i][j] = '.';
}
}
}
int sum = 1;
memset(flag, false, sizeof(flag));
road t;
t.x = sx, t.y = sy;
if (sx == 0) {
t.dir = 1;
}
else if (sx == n - 1) {
t.dir = 3;
}
else if (sy == 0) {
t.dir = 0;
}
else {
t.dir = 2;
}
dfs(t, 0, sum);
printf("%d ", sum);
sum = 1;
memset(flag, false, sizeof(flag));
if (sx == 0) {
t.dir = 3;
}
else if (sx == n - 1) {
t.dir = 1;
}
else if (sy == 0) {
t.dir = 2;
}
else {
t.dir = 0;
}
dfs(t, 1, sum);
printf("%d ", sum);
printf("%d\n",bfs());
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: