胜利大逃亡(续)(bfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1429
#include <stdio.h>
#include <queue>
#include <string.h>
using namespace std;
struct node
{
int x,y;
int state;
int step;
} s,t;
int Time,n,m;
int s_x,s_y,e_x,e_y;
char a[][];
int vis[][][];//标记状态
int dir[][] = {{,},{-,},{,-},{,}};
void bfs()
{
memset(vis,,sizeof(vis));
queue<node>q;
s.x = s_x;
s.y = s_y;
s.state = ;
s.step = ;
q.push(s);
vis[s_x][s_y][s.state] = ;
while(!q.empty())
{
t = q.front();
q.pop();
int x = t.x;
int y = t.y;
if (x==e_x&&y==e_y)
{
Time = t.step;
return ;
}
for (int i = ; i < ; i++)
{
int dx = x+dir[i][];
int dy = y+dir[i][]; if (dx>=&&dx<n && dy>=&&dy<m && (!vis[dx][dy][t.state]) && a[dx][dy]!='*')
{
if (a[dx][dy]=='.'||a[dx][dy]=='^'||a[dx][dy]=='@')
{
vis[dx][dy][t.state] = ;
s.x = dx;
s.y = dy;
s.step=t.step+;
s.state = t.state;
q.push(s); }
else if (a[dx][dy]>='A' && a[dx][dy]<='J')
{
if ((<<(a[dx][dy]-'A'))&t.state)//判断是否拿到过能打开当前门的钥匙
{
vis[dx][dy][t.state] = ;
s.x = dx;
s.y = dy;
s.step = t.step+;
s.state = t.state;
q.push(s);
}
}
else if (a[dx][dy]>='a' && a[dx][dy] <= 'j')
{
int state= ((<<(a[dx][dy]-'a'))|t.state);//更新此时拿到的钥匙的状态
if (!vis[dx][dy][state])
{
vis[dx][dy][state] = ;
s.x = dx;
s.y = dy;
s.step = t.step+;
s.state = state;
q.push(s);
} }
}
}
}
}
int main()
{
int t;
while(~scanf("%d%d%d%*c",&n,&m,&t))
{
Time = -;
for (int i = ; i < n; i++)
scanf("%s",a[i]);
for (int i = ; i < n; i++)
{
for (int j = ; j < m; j++)
{
if (a[i][j]=='@')
{
s_x = i;
s_y = j;
}
if (a[i][j]=='^')
{
e_x = i;
e_y = j;
}
}
}
bfs();
if (Time < t && Time!=-)
printf("%d\n",Time);
else
printf("-1\n");
}
return ;
}
胜利大逃亡(续)(bfs)的更多相关文章
- 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 Su ...
- hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了 题意:略过. 分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙.状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,k ...
- HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...
- hdu_1429_胜利大逃亡(续)(BFS状压)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 题意:迷宫的加强版,迷宫里有钥匙和门,问在指定的时间下能否逃出 题解:用二进制位来记录是否有该门 ...
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
- 胜利大逃亡(续)(状态压缩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 ...
- 胜利大逃亡(续)hdu1429(bfs)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
随机推荐
- mysql幻读
开启a,b两个数据库连接,a.b都开启事务后,b插入了一条数据R并提交,验证对a的操作造成的影响 select select for update update R update R 后 select ...
- Nginx 重新加载日志配置
最近在写一个nginx日志的切割脚本,切割完后,发现可以不重启服务,而直接重新加载日志配置文件的命令 [ kill -USR1 $nginx.pid ],但是不知道 -USR1这个参数是什么意 ...
- Centos6.6 安装nfs网络文件系统
一.介绍 nfs网络文件系统的,大部分用在内网文件共享,比如,对集群上传文件做共享,经常用在图片部分,当然数据量大了还是要做分离,做为专门的接口比较好,介绍一下基本安装环境: 1)Cnetos6.6 ...
- Scroll / Jump to id without jQuery
<scripttype="text/javascript"> function scroll(element){var ele = document.getElemen ...
- Linux快速入门教程-进程管理ipcs命令学习
使用Linux系统必备的技能之一就是Linux进程管理,系统运行的过程正是无数进程在运行的过程.这些进程的运行需要占用系统的内存等资源,做好系统进程的管理,对于我们合理分配.使用系统资源有非常大的意义 ...
- 【[Offer收割]编程练习赛14 D】剑刃风暴(半径为R的圆能够覆盖的平面上最多点数目模板)
[题目链接]:http://hihocoder.com/problemset/problem/1508 [题意] [题解] 求一个半径为R的圆能够覆盖的平面上的n个点中最多的点数; O(N2log2N ...
- xth 的玫瑰花(codevs 1360)
题目描述 Description 这天是rabbit 的生日前夕,Xth 来到花店,要给他的rabbit 买玫瑰花,为了保证质 量,他跟花店老板——小菜儿同学要求自己到花田采摘.小菜儿灰常希望早日见到 ...
- cogs 259. 亲戚
259. 亲戚 ★ 输入文件:relations.in 输出文件:relations.out 简单对比时间限制:1 s 内存限制:128 MB [问题描述] 或许你并不知道,你 ...
- 解决Ubuntu下Apache不解析PHP问题
这两天笔者遇到了一个很操蛋的问题——Apache无法解析PHP代码了,之前一直用的挺好的,突然就挂了,然后在网上疯狂的找解决办法,但是大都是php5的版本,而我却是7的版本,我就先顺便把5版本的解决方 ...
- 【Nginx】模块化设计
高度模块化的设计是Nginx的架构基础.全部模块都是以ngx_module_t结构体表示,该结构体内部定义了7个回调方法.它们负责模块的初始化和退出.commands成员是一个包括有ngx_comma ...