The Maze Makers
Time Limit: 1 Sec Memory Limit: 128 Mb Submitted: 167 Solved: 66Description
The Maze Makers is a publisher of puzzle books. One of their most popular series is maze books. They have a program that generates rectangular two-dimensional mazes like the one shown in Figure 1. The rules for these mazes are: (1) A maze has exactly two exterior cell walls missing, opening to two distinct terminal cells, (2) starting from any one cell, all other cells are reachable, (3) between any two cells in the maze there is exactly one simple path. Formally, a path is a sequence of cells where each cell and its successor on the path share an edge without a wall. A simple path is a path that never repeats a cell.
The Maze Maker program uses hexadecimal digits to encode the walls and passages of a maze. For each cell in the maze there is a corresponding hex digit. As shown in Figure 2, the 1's and 0's in the 4 digit binary representation of a hex digit correspond to the walls (1's) and passages (0's) for each cell in the maze. For example, the binary encoding for the hex digit B is 1011. Starting at the top of the cell and moving clockwise around it, this digit represents a cell with a wall at the top, a passage to the right and walls at the bottom and to the left. A path between two maze cells successively moves one cell up, down, left or right, going through passages only.
Figure 1: Sample Maze
Figure 2: Hex Code for Walls and Passageways
Figure 3: Maze with Cell Labels
Figure 3 shows the sample maze with the hexadecimal labels in each cell. For example, the hexadecimal digit E in the top-right cell indicates that it has a wall above it, to its right, below it, yet a passageway to its left. The hexadecimal digit 8 to its left indicates that its cell has only a wall above it. The inputs will always be self-consistent, in that the hexadecimal digits in neighboring cells will agree on whether they share a wall or passageway, and each input will always have precisely two terminal cells, each with one missing exterior wall.
Our sample maze is a legitimate maze in that all cells are reachable and there is a unique simple path between any pairs of cells in the maze. Your goal is to write a program that reads the hexadecimal descriptions of a potential maze and tests to determine if it is legitimate. If there is a problem, your program must report only the first problem, as detailed below in the section titled "Output".
Input
The input consists of the descriptions of one or more candidate mazes. Each maze description will start with two integers, H and W, indicating the height and width of the maze, respectively, such that 1 ≤ H ≤ 50 and 2 ≤ W ≤ 50. Following this first line will be H rows of hexadecimal digits, with each row consisting of W digits. The input is terminated with a line displaying a pair of zeros.
Output
For each candidate maze, the program should output the first one of the following statements that applies:
NO SOLUTION
UNREACHABLE CELL
MULTIPLE PATHS
MAZE OK
The classification statements are defined formally as follows:
NO SOLUTION - There is no path through the interior of the maze between the two exterior openings.
UNREACHABLE CELL - There is at least one cell in the maze that is not reachable by following passageways from either of the openings in the exterior walls of the maze.
MULTIPLE PATHS - There exists a pair of cells in the maze that have more than one simple path between them. Two simple paths are considered to be distinct if any part of the paths differ.
MAZE OK - None of the above problems exist.
Note well that for the second case given in the following examples, there is no path between the start and finish and there is an unreachable cell; the correct output should simply be NO SOLUTION, because that error message is listed first in the above list. Similarly, in the fourth example given, UNREACHABLE CELL is reported because that error has priority over the multiple paths.
Sample Input
6 7 9A8C98E 2E5753C 980A496 553C53C 53C75D5 3E3E363 3 3 F9A D3E 3AC 1 8 3AAA8AAE 6 3 9AC 3C5 A24 9A6 5BC 3C7 5 4 8A8E 592C 5186 161C 3A63 5 4 8AAE 59AC 5386 1E1C 3A63 0 0
Sample Output
MAZE OK NO SOLUTION MAZE OK UNREACHABLE CELL MULTIPLE PATHS MULTIPLE PATHS
Hint
Source
题意:给出一个地图,问能不能从起点走到终点,若不能输出NO SOLUTION,若能,则判断能不能走遍所有点,若不能输出UNREACHABLE CELL,若能,则判断是不是有多条路可以从起点走到终点,若有,输出MULTIPLE PATHS,否则输出MAZE OK
解题思路:用一个数组记录下每个格子哪些方向可以走,然后dfs即可,用数组vis记录一个点是否走过,若已经走过则说明有多条路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <vector>
#include <bitset>
#include <functional>
using namespace std;
#define LL long long
const int INF = 0x3f3f3f3f;
int n, m;
char ch[60][60];
int vis[60][60], a[60][60][4], flag, mul, cnt;
int dir[4][2] = { {1,0},{-1,0},{0,1},{0,-1} };
int sx, sy, ex, ey;
void dfs(int x, int y, int fax, int fay)
{
if (x == ex&&y == ey) flag = 1;
vis[x][y]++, cnt++;
for (int i = 0; i < 4; i++)
{
int xx = x + dir[i][0];
int yy = y + dir[i][1];
if (xx<1 || xx>n || yy<1 || yy>m || !a[x][y][i]) continue;
if (xx == fax&&yy == fay) continue;
if (vis[xx][yy]) { mul = 1; continue; }
dfs(xx, yy, x, y);
}
}
int main()
{
while (~scanf("%d%d", &n, &m) && (n + m))
{
memset(vis, 0, sizeof vis);
memset(ch, 0, sizeof ch);
memset(a, 0, sizeof a);
sx = 0;
for (int i = 1; i <= n; i++) scanf("%s", ch[i] + 1);
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= m; j++)
{
if ((ch[i][j] == '0' || ch[i][j] == '1' || ch[i][j] == '4' || ch[i][j] == '5' || ch[i][j] == '8' || ch[i][j] == '9' || ch[i][j] == 'C' || ch[i][j] == 'D') &&
(ch[i + 1][j] == 0 || ch[i + 1][j] == '0' || ch[i + 1][j] == '1' || ch[i + 1][j] == '2' || ch[i + 1][j] == '3' || ch[i + 1][j] == '4' || ch[i + 1][j] == '5' || ch[i + 1][j] == '6' || ch[i + 1][j] == '7'))
a[i][j][0] = 1;
if ((ch[i][j] == '0' || ch[i][j] == '1' || ch[i][j] == '2' || ch[i][j] == '3' || ch[i][j] == '4' || ch[i][j] == '5' || ch[i][j] == '6' || ch[i][j] == '7') &&
(ch[i - 1][j] == 0 || ch[i - 1][j] == '0' || ch[i - 1][j] == '1' || ch[i - 1][j] == '4' || ch[i - 1][j] == '5' || ch[i - 1][j] == '8' || ch[i - 1][j] == '9' || ch[i - 1][j] == 'C' || ch[i - 1][j] == 'D'))
a[i][j][1] = 1;
if ((ch[i][j] == '0' || ch[i][j] == '1' || ch[i][j] == '2' || ch[i][j] == '3' || ch[i][j] == '8' || ch[i][j] == '9' || ch[i][j] == 'A' || ch[i][j] == 'B') &&
(ch[i][j + 1] == 0 || ch[i][j + 1] == '0' || ch[i][j + 1] == '2' || ch[i][j + 1] == '4' || ch[i][j + 1] == '6' || ch[i][j + 1] == '8' || ch[i][j + 1] == 'A' || ch[i][j + 1] == 'C' || ch[i][j + 1] == 'E'))
a[i][j][2] = 1;
if ((ch[i][j] == '0' || ch[i][j] == '2' || ch[i][j] == '4' || ch[i][j] == '6' || ch[i][j] == '8' || ch[i][j] == 'A' || ch[i][j] == 'C' || ch[i][j] == 'E') &&
(ch[i][j - 1] == 0 || ch[i][j - 1] == '0' || ch[i][j - 1] == '1' || ch[i][j - 1] == '2' || ch[i][j - 1] == '3' || ch[i][j - 1] == '8' || ch[i][j - 1] == '9' || ch[i][j - 1] == 'A' || ch[i][j - 1] == 'B'))
a[i][j][3] = 1;
for (int k = 0; k < 4; k++)
if (!ch[i + dir[k][0]][j + dir[k][1]] && a[i][j][k])
{
if (sx) ex = i, ey = j;
else sx = i, sy = j;
}
}
}
cnt = flag = mul = 0;
dfs(sx, sy, 0, 0);
if (!flag) printf("NO SOLUTION\n");
else if (cnt < n*m) printf("UNREACHABLE CELL\n");
else if (mul) printf("MULTIPLE PATHS\n");
else printf("MAZE OK\n");
}
return 0;
}