hdu 1429 (bfs+状态压缩) 胜利大逃亡续
http://acm.hdu.edu.cn/showproblem.php?pid=1429
典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到了2号钥匙
那么原有的00000变为了00010,当到大了对应的二号门的时候,利用位运算00010来&上1<<2也就是00010就是非0,如果不是二号门的时候
那么00010&上1<<x都是0,所以此时其它门都进不去,如果再拿到了三号钥匙加上之后变成了00110,代表二号三号钥匙都拿到了,然后到了三号门的
时候00110&(1<<3)00100或者到了二号门的时候00110&(1<<2)00010都是非0的,到其他门都是为0的-----
这样就很好的解决了钥匙的记录问题,这里有10把钥匙,所以数组要大于2的10次方
code
#include<cstdio>
#include<queue>
#include<cstring>
using namespace std;
int dx[]={,-,,};
int dy[]={,,-,};
char dzm[]={'A','B','C','D','E','F','G','H','I','J'};
char xzm[]={'a','b','c','d','e','f','g','h','i','j'};
struct point {
int x,y;
int step,dir;
};
int n,m,sx,sy;
int visit[][][];//记录钥匙
char map[][];
int bfs()
{
int i,j;
memset(visit,,sizeof(visit));
queue<point>Q;
point now,next;
now.x=sx;now.y=sy;
now.step=now.dir=;
visit[now.x][now.y][now.dir]=;
Q.push(now);
while (!Q.empty())
{
now=Q.front();
Q.pop();
if (map[now.x][now.y]=='^')
return now.step;
for (i=;i<;i++)
{
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.step=now.step+;
next.dir=now.dir;
if (map[next.x][next.y]=='*')continue;
if (next.x<||next.x>n||next.y<||next.y>m)continue;
if (map[next.x][next.y]<='J'&&map[next.x][next.y]>='A')
{
for (j=;j<;j++)
{
if (map[next.x][next.y]==dzm[j])
{
if ((next.dir&(<<j))!=)
{
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
}
else if (map[next.x][next.y]<='j'&&map[next.x][next.y]>='a')
{
for (j=;j<;j++)
{
if (map[next.x][next.y]==xzm[j])
{
if ((next.dir&(<<j))==)
next.dir+=(<<j);
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
else
{
if (visit[next.x][next.y][next.dir]==)
{
visit[next.x][next.y][next.dir]=;
Q.push(next);
}
}
}
}
return -;
}
int main()
{
int i,j,t;
while (~scanf("%d %d %d",&n,&m,&t))
{
getchar();
for (i=;i<=n;i++)
{
for (j=;j<=m;j++)
{
scanf(" %c",&map[i][j]);
if (map[i][j]=='@')
sx=i,sy=j;
}
}
int q=bfs();
if (q>=t)
printf("-1\n");
else
printf("%d\n",q);
}
return ;
}
hdu 1429 (bfs+状态压缩) 胜利大逃亡续的更多相关文章
- hdu 1429(bfs+状态压缩)
题意:容易理解,但要注意的地方是:如果魔王回来的时候刚好走到出口或还未到出口都算逃亡失败.因为这里我贡献了一次wa. 分析:仔细阅读题目之后,会发现最多的钥匙数量为10把,所以把这个作为题目的突破口, ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdoj 1429 胜利大逃亡(续) 【BFS+状态压缩】
题目:pid=1429">hdoj 1429 胜利大逃亡(续) 同样题目: 题意:中文的,自己看 分析:题目是求最少的逃亡时间.确定用BFS 这个题目的难点在于有几个锁对于几把钥匙.唯 ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- 胜利大逃亡(续)(状态压缩bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- 胜利大逃亡(续)(bfs+状态压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- winform中datagridview刷新后的排序记忆
datagridview先点标题排序,但是重新刷新之后,还是变成窗体加载后的样子 我这里用定时器刷新的. 1.先定义三个全局变量 /// <summary> /// 需要排序的列和方向 / ...
- xshell连接不上linux情况一
设置网络的地址使用桥接方式
- Springboot 静态资源
说下默认映射的文件夹有: classpath:/META-INF/resources classpath:/resources classpath:/static classpath:/public ...
- angular记录
1. <h1>{{title}}</h1> 双花括号语法是 Angular 的插值绑定语法. 这个插值绑定的意思是把组件的 title 属性的值绑定到 HTML 中的 h1 标 ...
- .sh_history文件的管理机制
来源:http://www.aixchina.net/Article/27258 字数 1056阅读 4365评论 1赞 0 内容提要: .sh_history是在ksh中用于存储用户在shell中输 ...
- sql根据最小值去重
CREATE TABLE temp2 AS SELECT MAX(id) id FROM sys_oper_procenter GROUP BY pro_title 创建一个temp2的表 根据标题分 ...
- IDEA错误:Cannot start compilation: the output path is not specified for module "Test". Specify the out
错误是发生在从github上checkout自己的项目时.因为没有将配置文件一起上传,所以在运行Java程序时有了这个报错: Cannot start compilation: the output ...
- app和wap手机网站的区别在哪里
第一点 我们从依附的平台来看: 移动Wap网站:由移动设备的浏览器来支持,只要移动设备支持上网浏览网站基本上可以随时随地的打开网站查找自己需要的信息. 移动App客户端:由智能移动设备的操作系统来支持 ...
- 网页性能优化:防止JavaScript、CSS阻塞浏览器渲染页面
网页中引用的外部文件: JavaScritp.CSS 等常常会阻塞浏览器渲染页面.假设在 <head> 中引用的某个 JavaScript 文件由于各种不给力需要2秒来加载,那么浏览器渲染 ...
- 原生js,通过document.getElementByClassName获取元素的索引值
let itemList = document.getElementsByClassName('sky-item') // 一行所有元素 let index = 0 for(let i = 0; i& ...