hdu 4771——Stealing Harry Potter's Precious
来源:互联网 发布:店侦探类似软件 编辑:程序博客网 时间:2024/05/22 04:40
题意:遍历一个点格阵,方格中有一些宝物,要拿到所有的宝物,并且使时间最少
思路:bfs,状态压缩
代码如下:
#include<iostream>#include<cstdio>#include<cstring>#include<queue>using namespace std;struct pos{ int x,y; pos(int _x=-1,int _y=-1):x(_x),y(_y){} bool operator == (const pos &a) const{ return x==a.x&&y==a.y; }};struct sta{ pos s; int k; sta(pos s=pos(-1,-1),int k=0):s(s),k(k){}};const int dir[4][2]={-1,0,0,1,1,0,0,-1};char maze[105][105];int n,m;pos start;int key[105][105];int dp[105][105][1<<5];bool judge(int x,int y){ if(x<0||x>=n||y<0||y>=n)return false; return true;}void bfs(){ queue<sta> q; memset(dp,-1,sizeof(dp)); dp[start.x][start.y][0]=0; q.push(sta(start,0)); while(!q.empty()){ sta tmp=q.front(); q.pop(); for(int d=0;d<4;++d){ pos to=pos(tmp.s.x+dir[d][0],tmp.s.y+dir[d][1]); if(!judge(to.x,to.y))continue; if(maze[to.x][to.y]=='#')continue; int sk=tmp.k; if(maze[to.x][to.y]=='k'){ sk|=(1<<key[to.x][to.y]); } if(dp[to.x][to.y][sk]==-1||dp[to.x][to.y][sk]>dp[tmp.s.x][tmp.s.y][tmp.k]+1){ q.push(sta(to,sk)); dp[to.x][to.y][sk]=dp[tmp.s.x][tmp.s.y][tmp.k]+1; } } }}int main(){// freopen("data.txt","r",stdin); while(scanf("%d%d",&n,&m)!=EOF) { if(n==0&&m==0)break; for(int i=0;i<n;++i){ scanf("%s",maze[i]); for(int j=0;j<m;++j){ if(maze[i][j]=='@'){ start.x=i; start.y=j; } } } int K; scanf("%d",&K); for(int i=0;i<K;++i){ int a,b; scanf("%d%d",&a,&b); a--; b--; maze[a][b]='k'; key[a][b]=i; } for(int i=0;i<n;++i){ } bfs(); int ans=-1; for(int i=0;i<n;++i){ for(int j=0;j<m;++j){ if(maze[i][j]!='k')continue; if(dp[i][j][(1<<K)-1]==-1)continue; if(ans==-1||ans>dp[i][j][(1<<K)-1])ans=dp[i][j][(1<<K)-1]; } } printf("%d\n",ans); } return 0;}
0 0
- hdu 4771——Stealing Harry Potter's Precious
- hdu 4771 - Stealing Harry Potter's Precious
- HDU:4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- hdu 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- HDU 4771 Stealing Harry Potter's Precious
- JavaScript教程大纲
- JavaScript学习(六)
- Unity游戏开发实战视频教程
- Redis Twitter Example
- 线段树详细讲解
- hdu 4771——Stealing Harry Potter's Precious
- Hadoop Yarn 框架原理及运作机制
- ios 工程的缓存大小
- Shiro 报错 No SecurityManager accessible(汇总)
- 简洁但功能强大的EditPlus UltraEdit
- BF算法
- Jstl标签<c:if>的用法
- iostream: No such file or directory in Android NDK Environment
- 集合练习