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 ...
随机推荐
- [ios2]ASIHTTPReques 清除所有持久连接
http://www.winddisk.com/2012/08/27/iphone_screenlock_network_disconnection/ + (void)clearPersistentC ...
- Mybatis学习笔记(二) 之实现数据库的增删改查
开发环境搭建 mybatis 的开发环境搭建,选择: eclipse j2ee 版本,mysql 5.1 ,jdk 1.7,mybatis3.2.0.jar包.这些软件工具均可以到各自的官方网站上下载 ...
- iframe自适应高度问题
我页面中的iframe <iframe name="mainFrame" id="mainFrame" src="/account/${page ...
- CODEFORCES-PROBLEMSET
1A 水题 然而看不仔细爆int了 c++ #include <bits/stdc++.h> using namespace std; typedef long long ll; in ...
- 启用div作为编辑器 添加contentEditalbe属性
1.自从HTML5中新引入了contentEditalbe属性以后,div就与textarea一样,可以作为最常用的编辑器使用. 1.启用div作为编辑器 让div进入编辑状态很简单,只需要: 复制代 ...
- [ An Ac a Day ^_^ ][kuangbin带你飞]专题六 最小生成树 POJ 2031 Building a Space Station
最小生成树模板题 注意最后输出用%f (从C99开始%f已经不能用于输出double 即 输入用%lf 输出用%f) #include<cstdio> #include<algori ...
- python 算法练习
根据给定的线性函数来确定函数的表达形式: examples: get_function([0,1,2,3,4]) => f(x)=x get_function([1,4,7,10,13]) =& ...
- Win10下Mysql5.7.13,解压版安装流程
一.环境变量配置 1.将下载好的压宿包解压到安装目录,我的安装目录就是:D:\DevelopmentTool\Mysql5.7.13\mysql-5.7.13-winx64 2.鼠标选择计算机右键,点 ...
- ECStore图片存储采用阿里云OSS(图片存储)服务
主要功能:ECStore图片存储采用阿里云OSS(图片存储)服务 适用版本:ECStore 授权方式:授权域名使用,付费插件 联系方式: QQ 275553385 mail: jimingson ...
- json字符串和对象的相互转化
json在代码中是经常用到的,在此总结一下json字符串和对象及数组之间的相互转化: 1.javascript函数方式: <1> JSON.stringify :把一个对象转换成json字 ...