hdu 1429——胜利大逃亡(续)
来源:互联网 发布:阿里大数据应用 编辑:程序博客网 时间:2024/05/19 14:38
思路:状态压缩
代码如下:
#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){}};struct sta{ pos s; int k; sta(pos _s,int _k):s(_s),k(_k){}};const int dir[4][2]={-1,0,0,1,1,0,0,-1};int n,m,t;char maze[25][25];int key[15];pos start;pos stop;int dis[25][25][1<<11];inline bool judge(pos a){ if(a.x<0)return false; if(a.x>=n)return false; if(a.y<0)return false; if(a.y>=m)return false; return true;}int bfs(){ int ans=-1; memset(dis,-1,sizeof(dis)); dis[start.x][start.y][0]=0; queue<sta> q; 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))continue; if(maze[to.x][to.y]=='*')continue; int sk=tmp.k; char c=maze[to.x][to.y]; if(c>='A'&&c<='J'){ if(key[c-'A']==-1)continue; if(((int)(1<<key[c-'A'])&sk)==0)continue; else{ sta p=sta(to,sk); if(dis[to.x][to.y][sk]==-1||dis[to.x][to.y][sk]>dis[tmp.s.x][tmp.s.y][tmp.k]+1) { q.push(p); dis[to.x][to.y][sk]=dis[tmp.s.x][tmp.s.y][tmp.k]+1; } } } else if(maze[to.x][to.y]>='a'&&maze[to.x][to.y]<='j') { sk=(int)(sk|(1<<key[c-'a'])); sta p=sta(to,sk); if(dis[to.x][to.y][sk]==-1||dis[to.x][to.y][sk]>dis[tmp.s.x][tmp.s.y][tmp.k]+1){ q.push(p); dis[to.x][to.y][sk]=dis[tmp.s.x][tmp.s.y][tmp.k]+1; } } else if(dis[to.x][to.y][sk]==-1||dis[to.x][to.y][sk]>dis[tmp.s.x][tmp.s.y][tmp.k]+1){ q.push(sta(to,sk)); dis[to.x][to.y][sk]=dis[tmp.s.x][tmp.s.y][tmp.k]+1; if(c=='^'){ if(ans==-1||ans>dis[to.x][to.y][sk])ans=dis[to.x][to.y][sk]; } } } } return ans;}int main(){// freopen("data.txt","r",stdin); while(scanf("%d%d%d",&n,&m,&t)!=EOF) { int totkey=0; memset(key,-1,sizeof(key)); for(int i=0;i<n;++i) { scanf("%s",maze[i]); for(int j=0;j<m;++j){ if(maze[i][j]>='a'&&maze[i][j]<='j')key[maze[i][j]-'a']=totkey++; else if(maze[i][j]=='@'){ start.x=i; start.y=j; } else if(maze[i][j]=='^'){ stop.x=i; stop.y=j; } } } int ans=bfs(); printf("%d\n",ans<t?ans:-1); } 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 胜利大逃亡(续)
- hdu-1429 胜利大逃亡(续)
- HDU 1429 胜利大逃亡(续)
- Hdu 1429 胜利大逃亡(续)
- hdu 1429 胜利大逃亡(续)
- HDU--1429 胜利大逃亡(续)
- 1429 胜利大逃亡( 续 ) hdu
- HDU 1429 胜利大逃亡(续)
- .Net MVC5 Controller
- JavaScript入门篇之Cookie的应用
- Redis使用总结之与Memcached异同
- CODEVS 3145 汉诺塔游戏
- iOS开发流程——从注册开发者账号到提交应用
- hdu 1429——胜利大逃亡(续)
- UVA 109 || SCUD Busters(凸包面积计算
- Note On <Programming Entity Framework 2nd Edition> -05
- windows 7 下创建git ssh key
- js 单引号替换成双引号,双引号替换成单引号 操作
- 基本概念
- opencv 数据类型转换:CvArr, Mat, CvMat, IplImage, BYTE
- iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault)
- 实现isdigit