hdu_1072_Nightmare(BFS)
来源:互联网 发布:fifa数据库超级玩家 编辑:程序博客网 时间:2024/06/07 09:10
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1072
题意:给你一个地图,让你在炸弹爆之前找到出口,最初炸弹设定为6,每走一格需要1,中途有地方能让炸弹的时间重置为6,找到出口的最短时间。
题解:直接上BFS,需要注意的是要另开一个数组来存当前位置的炸弹最大时间,如果走回来时炸弹比原来的时间小,就不走。
#include<cstdio>#include<queue>using namespace std;int g[10][10],v[10][10],m,n,sx,sy,dir[][2]={1,0,-1,0,0,1,0,-1};#define FFC(i,a,b) for(int i=a;i<=b;i++)bool check(int x,int y){if(g[x][y]==0||x>n||x<1||y>m||y<1)return 0;return 1;}struct node{int x,y,c,t;};int fuck(){node s,now;s.x=sx,s.y=sy,s.c=0,s.t=6,v[sx][sy]=1;queue<node>Q;Q.push(s);while(!Q.empty()){now=Q.front();Q.pop();if(!now.t)continue;if(g[now.x][now.y]==3)return now.c;if(g[now.x][now.y]==4)now.t=6;v[now.x][now.y]=now.t;FFC(i,0,3){int xx=now.x+dir[i][0],yy=now.y+dir[i][1];if(check(xx,yy)&&now.t-1>v[xx][yy]){s.x=xx,s.y=yy,s.c=now.c+1,s.t=now.t-1;Q.push(s);}}}return -1;}int main(){int t,tmp;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);FFC(i,1,n)FFC(j,1,m){scanf("%d",&g[i][j]);v[i][j]=0;if(g[i][j]==2)sx=i,sy=j;}printf("%d\n",fuck());}return 0;}
0 0
- hdu_1072_Nightmare(BFS)
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- bfs
- BFS
- BFS
- bfs
- BFS
- BFS
- BFS
- BFS
- BFS
- bfs
- BFS
- vim 配置(ma6174 + YCM)
- NYOJ 914 Yougth的最大化(贪心,二分)
- List (单链表17个函数讲解)
- Lock与synchronized 的区别
- 不使用循环和乘除算前n项和
- hdu_1072_Nightmare(BFS)
- Maven学习13之覆盖率测试
- JSP中使用SpringBoot Security步骤
- Part2:Unity学习笔记十一 - Space Shooter
- AngularJS directive指令之require部分
- RabbitMQ 集群与高可用配置
- demo短信拦截---BroadcastReceiver
- View.setBackgroundColor(int color)
- Android NDK开发Crash错误定位