POJ 3083 Children of the Candy Corn
2016-03-30 13:05
204 查看
Children of the Candy Corn
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
Sample Output
题意:
先沿着左边的墙从 S 一直走,求到达 E 的步数。
再沿着右边的墙从 S 一直走,求到达 E 的步数。
最后求最短路。
分析:
最短路好办,关键是沿着墙走不太好想。
但只要弄懂如何转,这题就容易了。
单就沿着左走看一下:
当前方向 检索顺序
↑ : ← ↑ → ↓
→ : ↑ → ↓ ←
↓ : → ↓ ← ↑
← : ↓ ← ↑ →
如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。
在DFS时,加一个参数,用来保存当前的方向。
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12082 | Accepted: 5182 |
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 的步数。
再沿着右边的墙从 S 一直走,求到达 E 的步数。
最后求最短路。
分析:
最短路好办,关键是沿着墙走不太好想。
但只要弄懂如何转,这题就容易了。
单就沿着左走看一下:
当前方向 检索顺序
↑ : ← ↑ → ↓
→ : ↑ → ↓ ←
↓ : → ↓ ← ↑
← : ↓ ← ↑ →
如此,规律很明显,假设数组存放方向为 ← ↑ → ↓, 如果当前方向为 ↑, 就从 ← 开始依次遍历,找到可以走的,如果 ← 可以走,就不用再看 ↑ 了。
在DFS时,加一个参数,用来保存当前的方向。
#include <cstdio> #include <cstring> #include <queue> using namespace std; const int INF = 100000000; typedef pair<int, int> P; int t, w, h; char maze[50][50]; int d[50][50]; int sx, sy, gx, gy; int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1}; int dl[][2] = {{0, -1}, {-1, 0}, {0, 1}, {1, 0}}; int dr[][2] = {{0, 1}, {-1, 0}, {0, -1}, {1, 0}}; int dfs(int x, int y, int d0, int step, int dir[][2]) { for (int i = 0; i < 4; i++){ int j = ((d0 - 1 + 4) % 4 + i) % 4; int nx = x + dir[j][0]; int ny = y + dir[j][1]; if (nx == gx && ny == gy) return step + 1; if (nx >= 0 && nx < h && ny >= 0 && ny < w && maze[nx][ny] != '#') return dfs(nx, ny, j, step + 1, dir); } } int bfs() { queue<P> que; for (int i = 0; i < h; i++){ for (int j = 0; j < w; j++){ d[i][j] = INF; } } que.push(P(sx, sy)); d[sx][sy] = 0; while (que.size()){ P p = que.front(); que.pop(); if (p.first == gx && p.second == gy) break; for (int i = 0; i < 4; i++){ int nx = p.first + dx[i], ny = p.second + dy[i]; if (nx >= 0 && nx < h && ny >= 0 && ny < w && maze[nx][ny] != '#' && d[nx][ny] == INF){ que.push(P(nx, ny)); d[nx][ny] = d[p.first][p.second] + 1; } } } return d[gx][gy]; } int main() { int d1, d2; scanf("%d", &t); while (t--){ scanf("%d%d", &w, &h); for (int i = 0; i < h; i++){ scanf("%s", &maze[i]); } for (int i = 0; i < h; i++){ for (int j = 0; j < w; j++){ if (maze[i][j] == 'S'){ sx = i; sy = j; } if (maze[i][j] == 'E'){ gx = i; gy = j; } } } if (sx == 0){ d1 = 3; d2 = 3; } else if (sx == h - 1){ d1 = 1; d2 = 1; } else if (sy == 0){ d1 = 2; d2 = 0; } else if (sy = w - 1){ d1 = 0; d2 = 2; } printf("%d ", dfs(sx, sy, d1, 1, dl)); printf("%d ", dfs(sx, sy, d2, 1, dr)); printf("%d\n", bfs() + 1); } return 0; }
相关文章推荐
- 致联系报考我免试研究生的同学们-山世光 研究员-中科院计算所
- 《构建之法》阅读笔记--1
- JS正则表达式大全(整理详细且实用)
- Exception_android_java.lang.NoClassDefFoundError
- Zabbix之配置文件详解
- lintcode-medium-Maximum Subarray II
- 某基础的组合问题
- MySQL数据库主主
- Winform以任意角度旋转PictureBox中的图片的方法
- 链表的增删、查找以及逆序
- paddinglfet和marginleft区别
- 用Xocde搭建python开发环境
- 短信问题排查
- 十分钟搞懂什么是CGI(转)
- 迷宫的最短路径
- linux 命令快捷键(xshell环境下)
- 使用Android Studio的Live Templates——实现用少量的代码写出更多的代码
- HTTP真的很简单(转)
- Debug Assertion Failed Expression: _pFirstBlock==pHead
- 数据库并发学习总结