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. sed常见用法,删除匹配行的上2行,下3行

    删除匹配的下一行到最后一行 [root@test200 ~]# cat test a b c d e f [root@test200 ~]# sed '/c/{p;:a;N;$!ba;d}' test ...

  2. CMD杀进程 例如:杀8080端口的进程

    首先:端口被占用的报错形式如下 说明8080端口被占用 解决方案一:查找pid,根据pid去任务管理器的进程中结束占用8080端口号的进程 1.首先按快捷键windows+R,在运行框里输入cmd,如 ...

  3. 国内下载比较快的Maven仓库镜像

    #收集的比较快的maven仓库 http://maven.wso2.org/nexus/content/groups/public/ http://jcenter.bintray.com/http:/ ...

  4. Linux环境快速部署Zookeeper集群

    一.部署前准备: 1.下载ZooKeeper的安装包: http://zookeeper.apache.org/releases.html 我下载的版本是zookeeper-3.4.9. 2.将下载的 ...

  5. TCP/IP协议中backlog参数

    TCP建立连接是要进行三次握手,但是否完成三次握手后,服务器就处理(accept)呢? backlog其实是一个连接队列,在Linux内核2.2之前,backlog大小包括半连接状态和全连接状态两种队 ...

  6. 记JavaScript的入门学习(三)

    2016.12.6晚上十点半完成JavaScript的第二章学习,看了点第三章的开头总述,都说原生js每一个知识点都可以分分钟钟让你放弃,而我在努力探索着.月末的时候就回家放假了,希望在家也可以有个小 ...

  7. angular-ui-bootstrap插件API - Pagination

    Pagination: 案例 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <h ...

  8. 64位Java开发平台的选择,如何区分JDK,Tomcat,eclipse的32位与64版本

    当你想下载Linux.JDK.Tomcat.eclipse时,你是下载32位版本还是64位版本?64位版本有两种,应该选哪一个? 当你看到这些内容:x86.x64.x86-32.x86-64.ia64 ...

  9. Web前端优质学习网站

    * 官方:           W3C:http://www.w3.org/           ECMA:http://www.ecmascript.org/           Mozilla:h ...

  10. XTU 1243 2016

    $2016$长城信息杯中国大学生程序设计竞赛中南邀请赛$A$题 循环节. 循环节为$2016$,从数据范围以及题目中的一句话也能间接的体会出应该是有循环节的,并且循环节可能是$2016$. Feel ...