Maze
Maze
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=5094
BFS+状态压缩
把身上携带钥匙的状态压缩成一个2^10的整数。这道题难在如何表示墙和门所在的位置,我是另开了个两个N*N的数组mp_r[N][N],mp_c[N][N]分别以行和列错位存储存储墙和门的位置(mp_r[i][j]表示第r行j列和第r行j+1列的墙的状态),其他人好像是用mp[N][N][4]来存储墙和门的状态的,突然觉得我好蠢...这题坑点是一个位置可以存多把钥匙(吐血 怪不得这么多次都是WA)= =
代码如下:
#include<cstdio>
#include<queue>
#include<cstring>
#define LL long long
#define N 55
using namespace std;
struct node{
LL x,y,time;
bool key[];
};
node s,e;
LL mp_r[N][N];
LL mp_c[N][N];
LL key[N][N][];
LL n,m,p;
bool mark[][N][N];
bool flag;
LL dx[]={-,,,};
LL dy[]={,,-,};
LL zip(node t){
LL code=;
for(LL i=;i<p;++i)
code=(code<<)|t.key[i];
return code;
}
void init(){
flag=;
s.x=,s.y=,s.time=;
e.x=n,e.y=m,e.time=;
memset(mp_r,-,sizeof(mp_r));
memset(mp_c,-,sizeof(mp_c));
memset(key,,sizeof(key));
memset(mark,,sizeof(mark));
mark[][][]=;
LL k;
scanf("%I64d",&k);
while(k--){
LL x1,y1,x2,y2,g;
scanf("%I64d%I64d%I64d%I64d%I64d",&x1,&y1,&x2,&y2,&g);
if(x1-x2==)mp_r[x1][(y1+y2)/]=g;
else mp_c[(x1+x2)/][y1]=g;
}
scanf("%I64d",&k);
while(k--){
LL x,y,q;
scanf("%I64d%I64d%I64d",&x,&y,&q);
q--;
key[x][y][q]=;
}
for(int i=;i<;i++)
if(key[][][i])s.key[i]=;
}
int main(void){
while(~scanf("%I64d%I64d%I64d",&n,&m,&p)){
init();
queue<node> que;
que.push(s);
while(!que.empty()){
node t=que.front();
que.pop();
if(t.x==e.x&&t.y==e.y){
e.time=t.time;
flag=;
break;
}
for(LL i=;i<;++i){
LL x=t.x+dx[i];
LL y=t.y+dy[i];
if(<=x&&x<=n&&<=y&&y<=m){
node temp;
temp.x=x,temp.y=y,temp.time=t.time+;
for(LL j=;j<p;++j)temp.key[j]=t.key[j];
for(int j=;j<;j++)
if(key[x][y][j])temp.key[j]=;
if(dx[i]){
if(mp_c[(x+t.x)/][y]!=)
if(mp_c[(x+t.x)/][y]==-||temp.key[mp_c[(x+t.x)/][y]-]){
LL key_zip=zip(temp);
if(!mark[key_zip][x][y]){
mark[key_zip][x][y]=;
que.push(temp);
}
}
}else if(dy[i]){
if(mp_r[x][(y+t.y)/]!=)
if(mp_r[x][(y+t.y)/]==-||temp.key[mp_r[x][(y+t.y)/]-]){
LL key_zip=zip(temp);
if(!mark[key_zip][x][y]){
mark[key_zip][x][y]=;
que.push(temp);
}
}
}
}
}
}
if(flag)printf("%I64d\n",e.time);
else printf("-1\n");
}
}
Maze的更多相关文章
- Backtracking algorithm: rat in maze
Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...
- (期望)A Dangerous Maze(Light OJ 1027)
http://www.lightoj.com/volume_showproblem.php?problem=1027 You are in a maze; seeing n doors in fron ...
- 1204. Maze Traversal
1204. Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...
- uva705--slash maze
/*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...
- HDU 4048 Zhuge Liang's Stone Sentinel Maze
Zhuge Liang's Stone Sentinel Maze Time Limit: 10000/4000 MS (Java/Others) Memory Limit: 32768/327 ...
- Borg Maze(MST & bfs)
Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9220 Accepted: 3087 Descrip ...
- poj 3026 bfs+prim Borg Maze
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9718 Accepted: 3263 Description The B ...
- HDU 4035:Maze(概率DP)
http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description When w ...
- POJ 3026 : Borg Maze(BFS + Prim)
http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 9971 Accepted: 3347 Description The B ...
随机推荐
- Java中Animal b = new Dog();Dog c = new Dog();的区别
由于在编译阶段,只是检查参数的引用类型.然而在运行时,Java虚拟机(JVM)指定对象的类型并且运行该对象的方法.因此在下面的例子中,b.move()之所以能编译成功,是因为Animal类中存在mov ...
- floor()函数 向下取整 ceil()函数向上取整
floor(x) is the largest integer not greater than x , 也就是,floor(x) 返回的是小于等于x的所有整数中最大的整数,简单的说,就是去掉x的小 ...
- grunt--自动化打包工具使用
用grunt搭建自动化的web前端开发环境-完整教程 jQuery在使用grunt,bootstrap在使用grunt,百度UEditor在使用grunt,你没有理由不学.不用! 1. 前言 各位we ...
- 怎样通过WireShark抓到的包分析出SIP流程图
WireShark抓到了SIP包, 逐条分析, 看瞎...希望能够写个脚本, 自动生成流程图
- Could not locate executable E:\SoftWave\Hadoop-2.2.0\bin\winutils.exe in the Hadoop binaries解决办法
需要下载windows版本 bin目录下的文件,替换hadoop目录下原来的bin目录下的文件.下载网址是: https://github.com/srccodes/hadoop-common-2.2 ...
- Ubuntu中Qt+opencv图像显示
先抛出一个疑问,知道的希望留言解答下 (创建Application->Qt Console Application,代码与下面2.3一样,运行有时会显示图片,但大多数不显示,为什么?) 1.打开 ...
- SystemInfo获取设备系统参数
using UnityEngine; using System.Collections; using System.Collections.Generic; publicclassGameContro ...
- Chapter 20_1 table库
table库是由一些辅助函数构成,把table作为数组来操作,所有的函数都忽略传入参数的那张表中的非数字键. 无论如何,若一个操作需要取表的长度,这个表必须是一个真序列,或是拥有__len元方法. 提 ...
- Tyvj-超级书架
描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= N < ...
- centos redis 安装
# wget http://download.redis.io/releases/redis-2.8.6.tar.gz # tar xzf redis-2.8.6.tar.gz # cd redis- ...