hdu 4771 Stealing Harry Potter's Precious(BFS+状态压缩)
来源:互联网 发布:jb是什么意思网络用语 编辑:程序博客网 时间:2024/06/12 01:39
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=4771
题目大意:
在一个地图上面有一些宝藏,现在从某一个点出发要拿到所有的宝藏,问最短所需要走的步数是多少。
思路:
BFS+状态压缩。
在原来的vis[][]基础上添加一个状态,vis[i][j][state]代表在(i,j)上面拿了多少个宝藏时候的最小步数。
题目范围比较小,宝藏最多有4个,所以我们可以将宝藏分别设为1,2,4,8(二进制)。这样我们就可以进行状态压缩了,任意的两个进行&运算就可以知道是否已经被拿过了,如果没拿过就用|运算添加。
代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<iostream>#include<queue>using namespace std;int n,m,si,sj,sum;char s[105][105];int vis[105][105][35],dir[4][2]={0,1,0,-1,1,0,-1,0},maze[105][105];struct node{int x,y,step,state;}p[105];queue<node>qu;int bfs(){ while(!qu.empty()) qu.pop(); node now,next; now.x=si; now.y=sj; now.step=0; now.state=maze[si][sj]; qu.push(now); while(!qu.empty()) { now=qu.front(); qu.pop(); if(now.state==sum)return now.step; for(int i=0;i<4;i++) { next=now; next.x=now.x+dir[i][0]; next.y=now.y+dir[i][1]; next.step=now.step+1; if(maze[next.x][next.y]==-1)continue; if(next.x<=0||next.y<=0||next.x>n||next.y>m)continue; if(maze[next.x][next.y]>0&&(now.state&maze[next.x][next.y])==0) //如果宝藏还没被拿过,就拿进去。 next.state=now.state|maze[next.x][next.y]; if(vis[next.x][next.y][next.state])continue; else { vis[next.x][next.y][next.state]=1; qu.push(next); } } } return -1;}int main(){ int i,j,k,q; while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; sum=0; memset(maze,0,sizeof(maze)); for(i=1;i<=n;i++) for(j=1;j<=m;j++) { cin>>s[i][j]; if(s[i][j]=='@'){ maze[i][j]=0; si=i;sj=j; } if(s[i][j]=='#'){ maze[i][j]=-1; } if(s[i][j]=='.'){ maze[i][j]=0; } } memset(vis,0,sizeof(vis)); scanf("%d",&q); for(i=1;i<=q;i++) { int fx,fy; scanf("%d%d",&fx,&fy); maze[fx][fy]=1<<(i-1); sum+=maze[fx][fy]; } // printf("%d\n",sum); int ans= bfs(); printf("%d\n",ans); }}
0 0
- hdu 4771 状态压缩+bfs Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious (bfs+状态压缩)
- HDU 4771 Stealing Harry Potter's Precious(bfs+状态压缩)
- HDU-#4771 Stealing Harry Potter's Precious(bfs+状态压缩)
- hdu 4771 Stealing Harry Potter's Precious(BFS+状态压缩)
- hdu 4771Stealing Harry Potter's Precious(状态压缩bfs)
- HDOJ 4771 Stealing Harry Potter's Precious(bfs + 状态压缩)
- hdu4771 Stealing Harry Potter’s Precious(状态压缩+bfs)
- hdu 4771 Stealing Harry Potter's Precious(BFS+DFS)
- HDU 4771 Stealing Harry Potter's Precious(BFS)
- HDU 4771 Stealing Harry Potter's Precious(BFS)
- hdu 4771 Stealing Harry Potter's Precious(DFS+BFS)
- hdu 4771 Stealing Harry Potter's Precious(bfs+dfs)
- hdu4771,Stealing Harry Potter's Precious,bfs,状态压缩
- hdu 4774 Stealing Harry Potter's Precious 状态压缩+bfs 解题报告
- hdu 4771 Stealing Harry Potter's Precious(bfs预处理&TSP)
- HDU-4771 Stealing Harry Potter's Precious DFS + BFS
- hdu 4771 Stealing Harry Potter's Precious(bfs)
- OC大作业_通讯录的实现
- 解决UBUNTU系统软件安装过程出现libdirectfb-1.2-9_1.2.10.0-5_amd64.deb无法安装的问题
- ionic tab选项卡置于顶部
- c#??操作符
- Java读写CSV格式文件(opencsv)
- hdu 4771 Stealing Harry Potter's Precious(BFS+状态压缩)
- 把ListView和GridView与AppBar实现上下联动
- Python编写微信打飞机小游戏(三)
- 数据结构实验之二叉树一:树的同构
- 浅谈Java读取Csv实践(javacsv)
- OC基础 :Block(据说是重难点) 数组高级
- [转]程序员练手小项目
- 关于在android开发中使用jsoup的详解
- JAVA中使用AES加密