csu - 1566: The Maze Makers (bfs)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1566
题意还是蛮难懂的,至少对于我来说,需要认真读题。
输入矩阵的每一个数字换成2进制后,顺时针围一圈,用1表示墙,0表示空,这样就可以表示出一个迷宫,现在就是判断这个迷宫属于4种类型中哪种类型。
参考了 大神博客。构图很难,并且想法跟以往的题都不一样。是一个好题。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <cstring>
#include <string>
#include <algorithm>
#include <string>
#include <set>
#include <functional>
#include <numeric>
#include <sstream>
#include <stack>
#include <map>
#include <queue>
#pragma comment(linker, "/STACK:102400000,102400000")
#define CL(arr, val) memset(arr, val, sizeof(arr)) #define ll long long
#define inf 0x7f7f7f7f
#define lc l,m,rt<<1
#define rc m + 1,r,rt<<1|1
#define pi acos(-1.0) #define L(x) (x) << 1
#define R(x) (x) << 1 | 1
#define MID(l, r) (l + r) >> 1
#define Min(x, y) (x) < (y) ? (x) : (y)
#define Max(x, y) (x) < (y) ? (y) : (x)
#define E(x) (1 << (x))
#define iabs(x) (x) < 0 ? -(x) : (x)
#define OUT(x) printf("%I64d\n", x)
#define lowbit(x) (x)&(-x)
#define Read() freopen("a.txt", "r", stdin)
#define Write() freopen("b.txt", "w", stdout);
#define maxn 1000000000
#define N 2510
#define mod 1000000000
using namespace std; int n,m;
int num[][],vis[][];
int dir[][]={,,,-,,,,,,,-,};
//这里必须要对应 是为了防止往回走
struct point
{
int a,b;
};
int main()
{
//freopen("a.txt","r",stdin);
char s[];
int x,y,nx,ny;
while(~scanf("%d%d",&n,&m))
{
if(!n&&!m) break;
for(int i=;i<=n;i++)
{
scanf("%s",s+);
for(int j=;j<=m;j++)
{
if(s[j]>=''&&s[j]<='') num[i][j]=s[j]-'';
else num[i][j]=s[j]-'A'+;
num[i][j]=~num[i][j]; //读入之后 按位取反 ,能走的变成1 不能走的变成0
}
}
x=; //找出 起点和终点
for(int i=;i<=n;i++) //第一列或者第m列 看左右边界
{
if(num[i][]&) //代表表示 num[i][1]的四位2进制数中最后一位是1 代表有缺口
{
if(!x) x=i,y=;
else nx=i,ny=;
}
if(num[i][m]&)
{
if(!x) x=i,y=m;
else nx=i,ny=m;
}
}
for(int i=;i<=m;i++) //同上,看上下边界
{
if(num[][i]&)
{
if(!x) x=,y=i;
else nx=,ny=i;
}
if(num[n][i]&)
{
if(!x) x=n,y=i;
else nx=n,ny=i;
}
}
// printf("%d %d %d %d\n",x,y,nx,ny);
memset(vis,,sizeof(vis));
int mul=;
queue<point>que;
point now,next;
now.a=x,now.b=y;
vis[now.a][now.b]=;
que.push(now);
while(!que.empty()) //扩展所有能到达的点
{
next=que.front();que.pop();
for(int i=;i<;i++)
{
if(vis[next.a][next.b]==dir[i][]) continue; //已经访问过,防止往回走,
if(num[next.a][next.b]&dir[i][]) //该方向能访问
{
now.a=next.a+dir[i][];
now.b=next.b+dir[i][];
if(now.a>=&&now.a<=n&&now.b>=&&now.b<=m)
{
if(vis[now.a][now.b]) mul=; //多次到达同一个点
else
{
if(dir[i][]==) vis[now.a][now.b]=; //赋值相反方向的值给vis,防止往回走,跟上面的判断对应
else if(dir[i][]==) vis[now.a][now.b]=;
else if(dir[i][]==) vis[now.a][now.b]=;
else if(dir[i][]==) vis[now.a][now.b]=;
que.push(now);
}
}
}
}
}
if(vis[nx][ny]) //能到出口
{
bool flag=;
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
if(!vis[i][j])
{
//printf("%d %d\n",i,j);
flag=;
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 ;
}
csu - 1566: The Maze Makers (bfs)的更多相关文章
- The Maze Makers(csu1566)
1566: The Maze Makers Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 90 Solved: 33[Submit][Status][ ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- POJ 3026 Borg Maze(bfs+最小生成树)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6634 Accepted: 2240 Descrip ...
- (POJ 3026) Borg Maze 最小生成树+bfs
题目链接:http://poj.org/problem?id=3026. Description The Borg is an immensely powerful race of enhanced ...
- Borg Maze(BFS+MST)
Borg Maze http://poj.org/problem?id=3026 Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 快速切题 poj 3026 Borg Maze 最小生成树+bfs prim算法 难度:0
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 2969 Descrip ...
- POJ 3026 --Borg Maze(bfs,最小生成树,英语题意题,卡格式)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16625 Accepted: 5383 Descri ...
- POJ3026 Borg Maze(Prim)(BFS)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12729 Accepted: 4153 Descri ...
随机推荐
- hihocoder offer收割编程练习赛10 C 区间价值
思路: 令v[l, r](0<= l <= r < n)表示区间[l,r]的价值,则长度为n的区间的价值最少为0,最多为n*(n-1)/2.整体对价值二分,求能满足sum{v[l, ...
- 浏览器和ES5的介绍
浏览器的组成 : shell(浏览器的外壳).内核(渲染引擎.js引擎)主流浏览器及其内核: IE tritent(IE9及以下,IE10及以上用webkit) ...
- springmvc系列一 之配置介绍(包含官网doc)
1.springmvc 官网参考地址: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html 2 ...
- java JDK在windows及mac下安装配置
windows下安装: JDK下载 地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151. ...
- ASP.NET Excel下载方法一览
方法一 通过GridView(简评:方法比较简单,但是只适合生成格式简单的Excel,且无法保留VBA代码),页面无刷新 aspx.cs部分 using System; using System.Co ...
- UI概念体系要素
结构.渲染.交互.数据. 要素.呈现.交互 1)UI(组成)要素:结构 2)布局: 3)渲染: 4)事件处理: 5)数据:
- Android原生方式获取经纬度
两种定位方式:GPS定位.WiFi定位优劣: 如果项目定位要求较高还是建议使用三方地图库 GPS定位相比Wifi定位更精准且可在无网络情况下使用,但在室内基本暴毙无法使用WiFi定位没有室内外限制也不 ...
- laravel 只有/login路由403,如何解决
链接/login自动转跳到/login/导致找不到 /public/login/ 目录导致403; 将路由中\login改为\login1访问正常,但login依然403,而不是未找到路由 链接/lo ...
- PowerDesigner 操作手册
1.错误信息:Generation aborted due to errors detected during the verification of the model 解决方案: 把检查模型的选项 ...
- badblocks - 查询设备的坏区块
语法(SYNPSIS) badblocks [ -svwnf ] [ -b block-size ] [ -c blocks_at_once ] [ -i input_file ] [ -o outp ...