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 <cmath>
  4 #include <vector>
  5 #include <cstring>
  6 #include <string>
  7 #include <algorithm>
  8 #include <string>
  9 #include <set>
 10 #include <functional>
 11 #include <numeric>
 12 #include <sstream>
 13 #include <stack>
 14 #include <map>
 15 #include <queue>
 16 #pragma comment(linker, "/STACK:102400000,102400000")
 17 #define CL(arr, val)    memset(arr, val, sizeof(arr))
 18
 19 #define ll long long
 20 #define inf 0x7f7f7f7f
 21 #define lc l,m,rt<<1
 22 #define rc m + 1,r,rt<<1|1
 23 #define pi acos(-1.0)
 24
 25 #define L(x)    (x) << 1
 26 #define R(x)    (x) << 1 | 1
 27 #define MID(l, r)   (l + r) >> 1
 28 #define Min(x, y)   (x) < (y) ? (x) : (y)
 29 #define Max(x, y)   (x) < (y) ? (y) : (x)
 30 #define E(x)        (1 << (x))
 31 #define iabs(x)     (x) < 0 ? -(x) : (x)
 32 #define OUT(x)  printf("%I64d\n", x)
 33 #define lowbit(x)   (x)&(-x)
 34 #define Read()  freopen("a.txt", "r", stdin)
 35 #define Write() freopen("b.txt", "w", stdout);
 36 #define maxn 1000000000
 37 #define N 2510
 38 #define mod 1000000000
 39 using namespace std;
 40
 41 int n,m;
 42 int num[55][55],vis[55][55];
 43 int dir[4][3]={1,0,2,-1,0,8,0,1,4,0,-1,1};
 44 //这里必须要对应  是为了防止往回走
 45 struct point
 46 {
 47     int a,b;
 48 };
 49 int main()
 50 {
 51    //freopen("a.txt","r",stdin);
 52     char s[55];
 53     int x,y,nx,ny;
 54     while(~scanf("%d%d",&n,&m))
 55     {
 56         if(!n&&!m) break;
 57         for(int i=1;i<=n;i++)
 58         {
 59             scanf("%s",s+1);
 60             for(int j=1;j<=m;j++)
 61             {
 62                 if(s[j]>=‘0‘&&s[j]<=‘9‘) num[i][j]=s[j]-‘0‘;
 63                 else num[i][j]=s[j]-‘A‘+10;
 64                 num[i][j]=~num[i][j];   //读入之后 按位取反 ,能走的变成1 不能走的变成0
 65             }
 66         }
 67         x=0;   //找出  起点和终点
 68         for(int i=1;i<=n;i++)  //第一列或者第m列 看左右边界
 69         {
 70             if(num[i][1]&1)  //代表表示 num[i][1]的四位2进制数中最后一位是1 代表有缺口
 71             {
 72                 if(!x) x=i,y=1;
 73                 else nx=i,ny=1;
 74             }
 75             if(num[i][m]&4)
 76             {
 77                 if(!x) x=i,y=m;
 78                 else nx=i,ny=m;
 79             }
 80         }
 81         for(int i=1;i<=m;i++)  //同上,看上下边界
 82         {
 83             if(num[1][i]&8)
 84             {
 85                 if(!x) x=1,y=i;
 86                 else nx=1,ny=i;
 87             }
 88             if(num[n][i]&2)
 89             {
 90                 if(!x) x=n,y=i;
 91                 else nx=n,ny=i;
 92             }
 93         }
 94      //   printf("%d %d %d %d\n",x,y,nx,ny);
 95         memset(vis,0,sizeof(vis));
 96         int mul=0;
 97         queue<point>que;
 98         point now,next;
 99         now.a=x,now.b=y;
100         vis[now.a][now.b]=16;
101         que.push(now);
102         while(!que.empty())   //扩展所有能到达的点
103         {
104             next=que.front();que.pop();
105             for(int i=0;i<4;i++)
106             {
107                 if(vis[next.a][next.b]==dir[i][2]) continue; //已经访问过,防止往回走,
108                 if(num[next.a][next.b]&dir[i][2])  //该方向能访问
109                 {
110                     now.a=next.a+dir[i][0];
111                     now.b=next.b+dir[i][1];
112                     if(now.a>=1&&now.a<=n&&now.b>=1&&now.b<=m)
113                     {
114                         if(vis[now.a][now.b]) mul=1;  //多次到达同一个点
115                         else
116                         {
117                             if(dir[i][2]==4) vis[now.a][now.b]=1; //赋值相反方向的值给vis,防止往回走,跟上面的判断对应
118                             else if(dir[i][2]==1) vis[now.a][now.b]=4;
119                             else if(dir[i][2]==8) vis[now.a][now.b]=2;
120                             else if(dir[i][2]==2) vis[now.a][now.b]=8;
121                             que.push(now);
122                         }
123                     }
124                 }
125             }
126         }
127         if(vis[nx][ny]) //能到出口
128         {
129             bool flag=0;
130             for(int i=1;i<=n;i++)
131             {
132                 for(int j=1;j<=m;j++)
133                     if(!vis[i][j])
134                     {
135                         //printf("%d %d\n",i,j);
136                         flag=1;
137                         break;
138                     }
139                 if(flag) break;
140             }
141             if(flag) printf("UNREACHABLE CELL\n"); //有点不能到达
142             else
143             {
144                 if(mul) printf("MULTIPLE PATHS\n"); //多次到达
145                 else printf("MAZE OK\n"); //只有一条路径
146             }
147         }
148         else printf("NO SOLUTION\n"); //没有路径
149     }
150     return 0;
151 }
时间: 2024-10-23 17:28:46

csu - 1566: The Maze Makers (bfs)的相关文章

CSU 1566 The Maze Makers

这题....题意很长的.大体的意思就是给出一个迷宫,迷宫有16中状态.然后迷宫中每一个格子由一个十六进制数构成,然后把该数转换成2进制数,顺时针绕一圈表示的就是该格子四周的是否可以走,0表示可走,1表示不可走=_= 然后让你判断这个迷宫是属于4种类型的迷宫中哪种类型 每个迷宫保证只有一个入口和出口.而且相邻的格子之间只有一扇墙 四种迷宫分别是这样的 ①起点到终点不存在路径 ②迷宫中有存在有两个点不存在可通的路径 ③迷宫中存在两个点存在多条简单路径,其实就是判断一下图中是否存在环 ④以上都不是 其

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

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