hdu 1429
来源:互联网 发布:js获取标签的属性 编辑:程序博客网 时间:2024/06/06 00:09
解题思路:如果不考虑门和钥匙的情况,实际就是一个简单的bfs,现在加上每个点可能有钥匙和们的情况,我们只需加一维来表示当前的状态,当前的状态是指,我走到当前的一个点拿到钥匙的情况,即:dis[tx][ty][s],由于钥匙的数量的很小,我们可以用状态压缩的方式来表示状态即可(简单小节:此类题目实际就是将bfs简单的变形,加上状态压缩的方法,将简单的一维解法变为二维即可持,此种类型的题目还需加强训练,多做更有难度的题目)。
代码:
#include<cstdio>#include<queue>#include<cstring>#include<algorithm>using namespace std;int dir[4][2]={1,0,0,1,-1,0,0,-1};int n,m,t,sx,sy;bool isque[25][25][(1<<10)+5];char ma[25][25];struct node{ int x,y,s,tep;};queue<node>q;int bfs(){ node aa={sx,sy,0,0}; isque[sx][sy][0]=true; while(!q.empty()) q.pop(); q.push(aa); while(!q.empty()) { node tt=q.front(); //printf("%d %d %d\n",tt.x,tt.y,tt.s); q.pop(); for(int i=0;i<4;i++) { int tx=tt.x+dir[i][0]; int ty=tt.y+dir[i][1]; if(tt.tep+1==t) return -1; if(tx>=0&&tx<n&&ty>=0&&ty<m&&ma[tx][ty]!='*') { if(ma[tx][ty]=='.') { if(!isque[tx][ty][tt.s]) { isque[tx][ty][tt.s]=true; node ss={tx,ty,tt.s,tt.tep+1}; q.push(ss); } } else if(ma[tx][ty]>='a'&&ma[tx][ty]<='j') { int nows=tt.s|(1<<(ma[tx][ty]-'a')); if(!isque[tx][ty][nows]) { node ss={tx,ty,nows,tt.tep+1}; isque[tx][ty][nows]=true; q.push(ss); } } else if(ma[tx][ty]>='A'&&ma[tx][ty]<='J') { if((tt.s&(1<<(ma[tx][ty]-'A')))!=0) { if(!isque[tx][ty][tt.s]) { isque[tx][ty][tt.s]=true; node ss={tx,ty,tt.s,tt.tep+1}; q.push(ss); //printf("***%d\n",tt.s); } } } else if(ma[tx][ty]=='^') return tt.tep+1; } } } return -1;}int main(){ while(scanf("%d%d%d",&n,&m,&t)!=EOF) { for(int i=0;i<n;i++) { scanf("%s",ma[i]); } int fg=0; for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { if(ma[i][j]=='@') { sx=i; sy=j; fg=1; ma[sx][sy]='.'; break; } } if(fg) break; } memset(isque,false,sizeof(isque)); int ans=bfs(); printf("%d\n",ans); } return 0;}
0 0
- HDU 1429
- hdu 1429
- hdu 1429
- HDU 1429
- hdu 1429
- HDU 1429
- HDU-1429
- hdu 1429
- hdu 1429
- HDU 1429
- HDU 1429(BFS )
- hdu 1429搜索
- HDU 1429 搜索
- HDU 1429 BFS
- hdu 1429 状压bfs
- HDU 1429 BFS + 状压
- hdu 1429 状态搜索
- HDU 1429(状压+bfs)
- 经典算法之查找
- 【我就看看不说话】soap XML解析
- Android笔记3——Android的用户界面2
- 图片展示卡顿优化之范儿首页实战
- C/C++ 跨平台交叉编译、静态库/动态库编译、MinGW、Cygwin、CodeBlocks使用原理及链接参数选项
- hdu 1429
- 【LeetCode】Median of Two Sorted Arrays
- 直观理解线性代数的概念
- YTU:1301: Excel地址转换
- 数据倾斜
- Activity生命周期及应用场景
- 针对push到avd报错问题
- HDU 2612 Find a way(bfs)
- LeeCode 137 Single Number II