HDU-#4771 Stealing Harry Potter's Precious(bfs+状态压缩)
来源:互联网 发布:linux不显示电池 编辑:程序博客网 时间:2024/04/29 22:32
题目大意:给出k个宝物的坐标,从起点开始依次到达所有宝物的点取走宝物的最短时间。
解题思路:由于走过的点还可以走,并且要求取走所有的最短时间,因此利用bfs去搜索最短的步骤,用状态压缩来记录走过的状态,每次更新取走所有的花费。详见code。
题目来源:http://acm.hdu.edu.cn/showproblem.php?pid=4771
code:
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int MAXN = 100+10;const int inf = 0x3fffffff;int n,m,k,sx,sy,ans;int vis[MAXN][MAXN][10][50];char g[MAXN][MAXN];int dir[4][2]={1,0,0,1,-1,0,0,-1};struct node{ int x,y,step,cnt,status;};void bfs(){ queue<node> q; node s; q.push((node){sx,sy,0,0,0}); while(!q.empty()){ s=q.front();q.pop(); int x=s.x,y=s.y,d=s.step,ct=s.cnt,st=s.status; if(ct==k) ans=min(ans,d); if(vis[x][y][ct][st]!=0) continue; vis[x][y][ct][st]=1; for(int i=0;i<4;++i){ int tx=x+dir[i][0]; int ty=y+dir[i][1]; int tmp=g[tx][ty]-'0'; if(tmp>=0 && tmp<k){ if(1<<tmp & st) q.push((node){tx,ty,d+1,ct,st}); else q.push((node){tx,ty,d+1,ct+1,(1<<tmp) | st}); } else if(tx>0 && tx<=n && ty>0 && ty<=m && g[tx][ty]!='#') q.push((node){tx,ty,d+1,ct,st}); } }}int main(){ //freopen("in.txt","r",stdin); while(~scanf("%d%d",&n,&m) && n+m){ memset(g,0,sizeof(g)); memset(vis,0,sizeof(vis)); for(int i=1;i<=n;++i){ scanf("%s",g[i]+1); for(int j=1;j<=n;++j) if(g[i][j]=='@'){ sx=i;sy=j; } } scanf("%d",&k); int x,y; for(int i=0;i<k;++i){ scanf("%d%d",&x,&y); g[x][y]='0'+i; } ans=inf; bfs(); if(ans!=inf) printf("%d\n",ans); else printf("-1\n"); } return 0;}
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)
- 删除fragment_main.xml的方法
- 关于Android布局中 android:layout_centerHorizontal,android:layout_centerVertical的用法
- java.lang.SecurityException: class "org.hamcrest.TypeSafeMatcher"'s signer information does not matc
- Weights and Measures+uva+一般dp
- socket实例
- HDU-#4771 Stealing Harry Potter's Precious(bfs+状态压缩)
- 【解决方案】LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
- GNU工具链的使用(三)
- android 线程的开启和停止(借鉴java的技术)
- Surrounded Regions ,BFS
- cuda + vs assistant
- CellCtrl控件完美破解研究
- 从B树、B+树、B*树谈到R 树
- C# 格式化显示金额函数