CSU1566: The Maze Makers(BFS)

Description

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

题意:题意不难,就是看在构造迷宫后,对各种状况的判定,这题难度就在于构图吧,不过这个问题可以用二进制解决,每个方向的位左,下,右,上分别对应1,2,4,8,使用二进制1代表可行,0代表不可行即可

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;
#define ls 2*i
#define rs 2*i+1
#define up(i,x,y) for(i=x;i<=y;i++)
#define down(i,x,y) for(i=x;i>=y;i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define w(a) while(a)
#define LL long long
const double pi = acos(-1.0);
#define Len 100005

int n,m;
int maze[65][65],sx,sy,ex,ey,flag;
char str[1000];
int to[4][3] = {1,0,2,-1,0,8,0,1,4,0,-1,1},vis[65][65];

struct node
{
    int x,y;
};

int main()
{
    int i,j,k;
    while(~scanf("%d%d",&n,&m))
    {
        if(!n&&!m) break;
        mem(vis,0);
        up(i,1,n)
        {
            scanf("%s",str+1);
            up(j,1,m)
            {
                vis[i][j];
                if(str[j]>='0'&&str[j]<='9') maze[i][j] = str[j]-'0';
                else maze[i][j]=str[j]-'A'+10;
                maze[i][j]=~maze[i][j];//按位取反之后,除了不能走的路是0之外,其他位都是1了
            }
        }
        sx = 0;
        up(i,1,n)//对于每一行,我们看其左右边界,也就是和1,4相与的结果,为1的话证明这里是入口或者出口
        {
            if(maze[i][1]&1)
            {
                if(!sx) sx=i,sy=1;
                else ex=i,ey=1;
            }
            if(maze[i][m]&4)
            {
                if(!sx) sx=i,sy=m;
                else ex=i,ey=m;
            }
        }
        up(i,1,m)//同上,不过这里是对于每一行看上下边界
        {
            if(maze[1][i]&8)
            {
                if(!sx) sx=1,sy=i;
                else ex=1,ey=i;
            }
            if(maze[n][i]&2)
            {
                if(!sx) sx=n,sy=i;
                else ex=n,ey=i;
            }
        }
        int mul=0;
        queue<node> Q;
        node a,next;
        a.x = sx;
        a.y = sy;
        Q.push(a);
        vis[sx][sy] = 16;
        while(!Q.empty())
        {
            a = Q.front();
            Q.pop();
            up(i,0,3)
            {
                if(vis[a.x][a.y]==to[i][2])continue;//所在点往to[i][2]方向已经访问过了
                if(maze[a.x][a.y]&to[i][2])//该方向能够访问
                {
                    next.x = a.x+to[i][0];
                    next.y = a.y+to[i][1];
                    if(next.x>=1 && next.x<=n && next.y>=1 && next.y<=m)
                    {
                        if(vis[next.x][next.y]) mul=1;
                        else
                        {
                            if(to[i][2]==1) vis[next.x][next.y] = 4;
                            else if(to[i][2]==4) vis[next.x][next.y] = 1;
                            else if(to[i][2]==2) vis[next.x][next.y] = 8;
                            else if(to[i][2]==8) vis[next.x][next.y] = 2;
                            Q.push(next);
                        }
                    }
                }
            }
        }
        if(vis[ex][ey])//能走到出口
        {
            flag = 0;
            up(i,1,n)
            {
                up(j,1,m)
                {
                    if(!vis[i][j])
                    {
                        flag = 1;
                        break;
                    }
                }
                if(flag)
                    break;
            }
            if(flag)//有一个点没访问到
                printf("UNREACHABLE CELL\n");
            else
            {
                if(mul) printf("MULTIPLE PATHS\n");//能出处并且有一个点能被重复访问,则输出多路
                else printf("MAZE OK\n");//只有一条路
            }
        }
        else//走不到出口
            printf("NO SOLUTION\n");
    }

    return 0;
}
时间: 2024-10-06 16:32:53

CSU1566: The Maze Makers(BFS)的相关文章

csu - 1566: The Maze Makers (bfs)

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566 题意还是蛮难懂的,至少对于我来说,需要认真读题. 输入矩阵的每一个数字换成2进制后,顺时针围一圈,用1表示墙,0表示空,这样就可以表示出一个迷宫,现在就是判断这个迷宫属于4种类型中哪种类型. 参考了 大神博客.构图很难,并且想法跟以往的题都不一样.是一个好题. 1 #include <iostream> 2 #include <cstdio> 3 #include <c

The Maze Makers

1566: The Maze Makers Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 90  Solved: 33[Submit][Status][Web Board] Description The Maze Makers is a publisher of puzzle books. One of their most popular series is maze books. They have a program that genera

poj3026Borg Maze(bfs预处理+最小生成树)

题目链接: 啊哈哈,点我点我 思路: 首先把图中的A S预处理出来,然后对这些点逐一做bfs找到这些点到其它点的最短路径,然后建图完毕也用最小生成树的prim算法或者kruscal算法求出连接所有点的最短距离..不知道为嘛用dis数组去维护为什么会超时,而在结构体里面用step数组却可以过,我也不知道为什么,纠结了很多天..我把错误的代码贴出来,希望各位帮我找出原因,不胜感激... 题目: Borg Maze Time Limit: 1000MS   Memory Limit: 65536K T

ZOJ 3420 Double Maze (BFS)

链接 :  http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3420 普通的BFS 两个图的状态表示成一个状态.记录答案直接用string保存操作. #include <iostream> #include <sstream> #include <cstring> #include <cstdio> #include <vector> #include <stac

HDU 5094 --Maze【BFS &amp;&amp; 状态压缩】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 903    Accepted Submission(s): 316 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

hdoj 5094 Maze 【BFS + 状态压缩】 【好多坑】

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 901    Accepted Submission(s): 314 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

poj 3026 Borg Maze 最小生成树+bfs prim算法

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 2969 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

Borg Maze(MST &amp; bfs)

Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to descr

POJ 3026 Borg Maze【BFS+最小生成树MST】

Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12014 Accepted: 3925 Description The Borg is an immensely powerful race of enhanced humanoids from the delta quadrant of the galaxy. The Borg collective is the term used to describe