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的更多相关文章

  1. Backtracking algorithm: rat in maze

    Sept. 10, 2015 Study again the back tracking algorithm using recursive solution, rat in maze, a clas ...

  2. (期望)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 ...

  3. 1204. Maze Traversal

    1204.   Maze Traversal A common problem in artificial intelligence is negotiation of a maze. A maze ...

  4. uva705--slash maze

    /*这道题我原本是将斜线迷宫扩大为原来的两倍,但是在这种情况下对于在斜的方向上的搜索会变的较容易出错,所以参考了别人的思路后将迷宫扩展为原来的3倍,这样就变成一般的迷宫问题了*/ #include&q ...

  5. 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 ...

  6. Borg Maze(MST & bfs)

    Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9220   Accepted: 3087 Descrip ...

  7. poj 3026 bfs+prim Borg Maze

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9718   Accepted: 3263 Description The B ...

  8. HDU 4035:Maze(概率DP)

    http://acm.split.hdu.edu.cn/showproblem.php?pid=4035 Maze Special Judge Problem Description   When w ...

  9. POJ 3026 : Borg Maze(BFS + Prim)

    http://poj.org/problem?id=3026 Borg Maze Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  10. Borg Maze 分类: POJ 2015-07-27 15:28 5人阅读 评论(0) 收藏

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9971   Accepted: 3347 Description The B ...

随机推荐

  1. pb_ds(平板电视)整理

    有人说BZOJ3040用普通的<queue>中priority_queue搞dijkstra过不了. 我只想说你们的djk可能写的太丑了. 先上代码 #include<iostrea ...

  2. android 轮播图

    轮播图是很常用的一个效果 核心功能已经实现 没有什么特殊需求 自己没事研究的 所以封装的不太好 一些地方还比较糙 为想要研究轮播图的同学提供个参考 目前测试图片为mipmap中的图片 没有写从网络加载 ...

  3. 解决,Incorrect table definition; there can be only one auto column and it must be defined as a key

    今天在迁移项目时,操作数据库报错: Incorrect table definition; there can be only one auto column and it must be defin ...

  4. 数据持久层框架iBatis, Hibernate 与 JPA 比较

    在本文中我们介绍并比较两种最流行的开源持久框架:iBATIS和Hibernate,我们还会讨论到Java Persistence API(JPA).我们介绍每种解决方案并讨论其所规定的品质,以及在广泛 ...

  5. XMLHttpRequest上传文件实现进度条

    话不多说,直接上代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...

  6. 第12章 MySQL高级管理

    1.手动更新权限后,需向服务器指出已对权限进行修改: (在MySQL提示符下)flush privileges; 2.查看用户所拥有的权限: 如: show grants for bookorama; ...

  7. 打包apk java 虚拟机内存不足

    解决方案:在android->sdk->build-tools-android-version 中有个 dx.bat dx.bat --dex 命令的dx.bat脚本有这样一句代码 REM ...

  8. 免费SSL证书申请

    https://buy.wosign.com/free/#apply 和 http://www.startssl.com/

  9. 【IE6的疯狂之十一】CSS的优先级及!important在IE6下的BUG

    一 css的优先级 今天有人跟我说css hack中用!important来区分ie6,因为ie6不支持!important,是的在很早以前我也是用过这种方法写hack,但是后来就基本不用了.本来我对 ...

  10. MFC 自定义控件

    需要在MFC实现自定义控件功能,网上搜集找的如下方法实现: 以下是步骤说明. 一.自定义一个空白控件  1.先创建一个MFC工程 NEW Project-->MFC-->MFC Appli ...