hdu 1429 胜利大逃亡(续) (bfs+状态压缩)
又开始刷题了
题意:略过。
分析:主要是确定状态量,除了坐标(x,y)之外,还有一个key状态,就好比手上拿着一串钥匙。状态可以用位运算来表示:key&(x,y)表示判断有没有这扇门的钥匙,key|(x,y)表示捡起了一把钥匙。
错误:1、开标记数组mark[][][],key状态大小顺手开成key,其实应该是1<<key
2、判断应该先判wall(),顺序颠倒倒是RE(访问越界)
3、key<=10:只有 a-j 共10种钥匙
思考:这道题其实应该是有漏洞的,因为魔王只是查看男主是否在原位置上,并没有说明检查钥匙,如此一来男主只要在时间限定T之内找到一把钥匙,就向逃亡迈进了一步,换句话说就是状态被大幅度增加——不再是必须在T内找到出口“^”。进一步说,若魔王发现男主不在原位置,不仅把他放回原位,还还原钥匙的位置,那么如果在时间限定T内,他能够找到钥匙并返回原位,又是否算是通过了这次检查呢?以上两种情况但凡一种成立,测试样例2都可以通过。
#include<cstdio>
#include<cstring>
#include<cctype>
#include<queue>
#include<algorithm>
using namespace std; const int MAXN=;
const int KEY=; int dir[][]={,-,,,-,,,}; struct Node{
int x,y,t,key;
Node(){}
Node(int _x,int _y,int _t,int _key):x(_x),y(_y),t(_t),key(_key){}
}; char g[MAXN][MAXN];
int mark[MAXN][MAXN][<<KEY];
queue<Node>q; int n,m,T; bool wall(int x,int y)
{
if(x<||x>=n||y<||y>=m)
return true;
if(g[x][y]=='*')
return true;
return false;
} int Num(char ch)
{
return ch-(isupper(ch)?'A':'a');
} int bfs(int sx,int sy)
{
while(!q.empty())
q.pop(); memset(mark,,sizeof(mark));
q.push(Node(sx,sy,,));
while(!q.empty())
{
Node e=q.front();q.pop(); if(e.t>=T)
break; for(int i=;i<;i++)
{
int dx=e.x+dir[i][];
int dy=e.y+dir[i][];
int dt=e.t+;
int dkey=e.key; char ch=g[dx][dy]; if(wall(dx,dy))
continue;
if(dt>=T||mark[dx][dy][dkey])
continue;
if(isupper(ch)&&!(dkey&(<<Num(ch))))
continue;
if(islower(ch))
dkey=dkey|(<<Num(ch)); if(g[dx][dy]=='^')
return dt; mark[dx][dy][dkey]=;
q.push(Node(dx,dy,dt,dkey));
}
}
return -;
} int main()
{
while(~scanf("%d%d%d",&n,&m,&T))
{
for(int i=;i<n;i++)
scanf("%s",g[i]); int sx,sy,ex,ey;
for(int i=;i<n;i++)
for(int j=;j<m;j++)
if(g[i][j]=='@'){
sx=i;
sy=j;
g[i][j]='.';
} printf("%d\n",bfs(sx,sy));
}
return ;
}
hdu 1429 胜利大逃亡(续) (bfs+状态压缩)的更多相关文章
- hdu - 1429 胜利大逃亡(续) (bfs状态压缩)
http://acm.hdu.edu.cn/showproblem.php?pid=1429 终于开始能够做状态压缩的题了,虽然这只是状态压缩里面一道很简单的题. 状态压缩就是用二进制的思想来表示状态 ...
- hdu 1429 胜利大逃亡(续)(bfs+位压缩)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- HDOJ 1429 胜利大逃亡(续) (bfs+状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1429 思路分析:题目要求找出最短的逃亡路径,但是与一般的问题不同,该问题增加了门与钥匙约束条件: 考虑 ...
- hdu.1429.胜利大逃亡(续)(bfs + 0101011110)
胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total S ...
- HDU 1429 胜利大逃亡(续)(bfs+状态压缩,很经典)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1429 胜利大逃亡(续)
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1429 胜利大逃亡(续) Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王 ...
- HDU 1429 胜利大逃亡(续)(DP + 状态压缩)
胜利大逃亡(续) Problem Description Ignatius再次被魔王抓走了(搞不懂他咋这么讨魔王喜欢)…… 这次魔王汲取了上次的教训,把Ignatius关在一个n*m的地牢里,并在地牢 ...
- HDU 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 S ...
随机推荐
- Difference Between Vector and Deque in C++
1) Dequeue can quickly insert or delete both at the front or the end. However, vector can only quick ...
- IOS快速集成下拉上拉刷新
http://code4app.com/ios/%E5%BF%AB%E9%80%9F%E9%9B%86%E6%88%90%E4%B8%8B%E6%8B%89%E4%B8%8A%E6%8B%89%E5% ...
- C# 设置程序开机自动运行(+注册表项)
有时候我们需要让软件安装好了,开机自动运行,这时我们需要把启动项加载到注册表中,需要注意的时现在很多杀毒软件在其他软件更改注册表的时候会有提示,可能会阻止.下面代码包含增加启动项到注册表和删除启动项. ...
- 用Stopwatch类获得程序运行时间
我们可以用Stopwatch类获得程序的运行时间,在优化代码时,可以用此方法来查看优化前后程序所耗费的时间 //Stopwatch类別在System.Diagnostics命名空间里 Stopwatc ...
- poj 2311 Cutting Game 博弈论
思路:求SG函数!! 代码如下: #include<iostream> #include<cstdio> #include<cmath> #include<c ...
- 网站建设底层知识Socket与Http解析
在进行网站建设的时候,常常遇到不同的协议,Socket和http协议都可以实现数据传输,但两种传输方式在网站建设中有什么各自的特点,和缺点,如何选择合适的传输方式. 1 数据传输方式 1.1 Soc ...
- ubuntu挂载磁盘
1.首先查磁盘UUID:sudo blkid 2.打开挂载文件:sudo /etc/fstab 3.写挂载文件: UUID=000860AE000FDD66 /mnt/disk1 ...
- 程序空间(Program memory)
The computer program memory is organized into the following: Data Segment (Data + BSS + Heap) Stack ...
- Hadoop基础教程-运行环境搭建
一.Hadoop是什么 一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序.充分利用集群的威力进行高速运算和存储. Hadoop实现了一个分布式 ...
- JavaPersistenceWithHibernate第二版笔记Getting started with ORM-002Domain层详解及M etaModel
一.结构 二.配置文件约定 The JPA provider automatically picks up this descriptor if you place it in a META-INF ...