状压BFS

注意在用二维字符数组时,要把空格、换行处理好。

#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
int sx,sy,C,n,m;
int ans;
bool vis[][][<<];
char g[][];
int key[][];
int dx[]={,,-,},dy[]={,,,-};
bool check(int a,int b){return <=a&&a<=n&&<=b&&b<=m;}
bool ok(int mask)
{
for(int i=;i<=C;i++)
if(mask&(<<i));
else return false;
return true;
}
struct mask
{
int x,y,key,step;
mask(int x,int y,int key,int step):x(x),y(y),key(key),step(step){};
};
queue<mask>q;
int bfs()
{//printf("%d %d",sx,sy);
while(q.size())q.pop();
memset(vis,false,sizeof(vis));
q.push(mask(sx,sy,,));
vis[sx][sy][]=true;
while(q.size())
{
mask tmp=q.front();q.pop();//printf("%d %d %d\n",tmp.x,tmp.y,tmp.key);
if(ok(tmp.key))
ans=min(ans,tmp.step);
for(int i=;i<;i++)
{
int nx=tmp.x+dx[i];
int ny=tmp.y+dy[i];
int nkey=tmp.key;//printf("%d %d\n",nx,ny);
if(check(nx,ny)&&!vis[nx][ny][nkey]&&g[nx][ny]!='#')
{
if(key[nx][ny])
nkey|=key[nx][ny];
vis[nx][ny][nkey]=true;
q.push(mask(nx,ny,nkey,tmp.step+));
}
}
}
return ans==INF?-:ans;
}
int main()
{
while(~scanf("%d %d",&n,&m)){
getchar();//这里的getchar()吸收掉换行,刚刚没注意这里WA后,想了半个钟,可恨啊。。。。
if(n==&&m==)break;
ans=INF;
memset(key,,sizeof(key));
for(int i=;i<=n;i++)
{
for(int j=;j<=m;j++)
{
scanf("%c",&g[i][j]);
if(g[i][j]=='@')
{
sx=i;
sy=j;
}
}
getchar();
}
/* for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++)
printf("%c",g[i][j]);
printf("\n");
}*/
scanf("%d",&C);
int a,b;
for(int i=;i<=C;i++)
{
scanf("%d %d",&a,&b);
key[a][b]=<<i;
}
printf("%d\n",bfs());
}
}

HDU Stealing Harry Potter's Precious(状压BFS)的更多相关文章

  1. HDU 4012 Paint on a Wall(状压+bfs)

    Paint on a Wall Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) ...

  2. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  3. HDU 4771 Stealing Harry Potter's Precious (2013杭州赛区1002题,bfs,状态压缩)

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  4. HDU 4771 Stealing Harry Potter's Precious dfs+bfs

    Stealing Harry Potter's Precious Problem Description Harry Potter has some precious. For example, hi ...

  5. 拯救大兵瑞恩 HDU - 4845(状压bfs || 分层最短路)

    1.状压bfs 这个状压体现在key上  我i们用把key状压一下  就能记录到一个点时 已经拥有的key的种类 ban[x1][y1][x2][y1]记录两个点之间的状态 是门 还是墙 还是啥都没有 ...

  6. POJ 1324 Holedox Moving (状压BFS)

    POJ 1324 Holedox Moving (状压BFS) Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 18091 Acc ...

  7. P2622 关灯问题II(状压bfs)

    P2622 关灯问题II 题目描述 现有n盏灯,以及m个按钮.每个按钮可以同时控制这n盏灯——按下了第i个按钮,对于所有的灯都有一个效果.按下i按钮对于第j盏灯,是下面3中效果之一:如果a[i][j] ...

  8. 状压BFS

    ​题意:1个机器人找几个垃圾,求出最短路径. 状压BFS,这道题不能用普通BFS二维vis标记数组去标记走过的路径,因为这题是可以往回走的,而且你也不能只记录垃圾的数量就可以了,因为它有可能重复走同一 ...

  9. 【HDU 4771 Stealing Harry Potter's Precious】BFS+状压

    2013杭州区域赛现场赛二水... 类似“胜利大逃亡”的搜索问题,有若干个宝藏分布在不同位置,问从起点遍历过所有k个宝藏的最短时间. 思路就是,从起点出发,搜索到最近的一个宝藏,然后以这个位置为起点, ...

随机推荐

  1. Docker实战部署应用——Redis

    Redis 部署 拉取Redis镜像 docker pull redis 创建Redis容器 docker run -id --name=sun_redis -p 6379:6379 redis 客户 ...

  2. Conference - open source drives IOT from device to edge

    Open source drives IOT from device to edge 以下都是针对IOT领域的项目: ACRN A Big Little Hypervisor for IoT Deve ...

  3. OGG-01161

    Bad column index (35) specified for table user.table_name, max columns = 35. 原因:源端表结构发生了变更 解决办法:1.如果 ...

  4. X-MAS CTF 2018 - Crypto - Hanukkah

    参考链接 https://ctftime.org/task/7321 https://github.com/VoidHack/write-ups/tree/master/X-MAS%20CTF%202 ...

  5. GNU linker script,ld script,GNU链接脚本

    https://blog.csdn.net/itxiebo/article/details/50937412 https://blog.csdn.net/itxiebo/article/details ...

  6. bzoj4399 魔法少女LJJ 线段树合并+线段树二分+并查集

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4399 题解 毒瘤题 \(9\) 种操作还有支持动态图的连通性 仔细读题 $ c<=7$. ...

  7. 左右flex布局

    修复版本

  8. struts2+jsp 遍历 <s:iterator><s:property>

    直接把list用request传到jsp页面 <s:iterator var="u" value="#request.users"> <tr& ...

  9. Linux启动redis提示 /var/run/redis_6379.pid exists, process is already running or crashed

    执行启动命令:service redisd start 提示信息:/var/run/redis_6379.pid exists, process is already running or crash ...

  10. AutoLayout面试题记录-用NSLayoutConstraint写动画

    import UIKit class ViewController: UIViewController { @IBOutlet weak var topY: NSLayoutConstraint! @ ...