HDU 1072(bfs)
来源:互联网 发布:估算网络机房电源容量 编辑:程序博客网 时间:2024/05/17 10:29
题意:主人公在一块6秒钟后要爆炸的范围内,主人公没走一步需要1秒,0是墙壁,1是路,2是起点,3是目标,4是爆炸时间从新设置,如果刚好在0秒到达3或者4都算输,每个可到达位置都可以重复到达,求主人公多短时间能够走出去,如果走不出去输出-1.
使用优先队列:
#include<iostream>#include<stdio.h>#include<queue>#include<cstring>using namespace std;int cas,n,m;int map[10][10],vis[10][10][7],dir[4][2]={1,0,-1,0,0,1,0,-1},ans;struct node{ int x,y,step,time; node(int _x=0,int _y=0,int _step=0,int _time=0):x(_x),y(_y),step(_step),time(_time){}; friend bool operator<(const node&a,const node&b) { return a.step>b.step; }};node tar;priority_queue <node > q;void BFS(){ while(!q.empty()) { node t=q.top(); q.pop(); if(t.time==1)continue; for(int k=0;k<4;k++) { int x=t.x+dir[k][0]; int y=t.y+dir[k][1]; if(x>=0&&x<n&&y>=0&&y<m&&!vis[x][y][t.time-1]&&map[x][y]!=0) { if(map[x][y]==1||map[x][y]==2) { vis[x][y][t.time-1]=1; q.push(node(x,y,t.step+1,t.time-1)); } if(map[x][y]==3) { vis[x][y][t.time-1]=1; ans=t.step+1;return; } if(map[x][y]==4) { vis[x][y][t.time-1]=1; q.push(node(x,y,t.step+1,t.time-1)); if(!vis[x][y][6]) { vis[x][y][6]=1; q.push(node(x,y,t.step+1,6)); } } } } } }int main(){ //freopen("1.txt", "r", stdin); //freopen("out1.txt", "w", stdout); scanf("%d",&cas); while(cas--) { while (!q.empty()) q.pop(); ans=-1; memset(vis,0,sizeof(vis)); scanf("%d%d",&n,&m); for(int i=0;i<n;i++) for(int j=0;j<m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) { q.push(node(i,j,0,6)); vis[i][j][6]=1; } } BFS(); printf("%d\n",ans); }}
不使用优先队列:
#include<iostream>#include<queue>using namespace std;int dir[][2]={0,1,1,0,0,-1,-1,0}; //4个方向struct node{ int x; int y; int time;//剩余的时间 int step;//走过的步数}begin;int n,m,i,j;int map[10][10]; //地图int mark[10][10];//标记时间void bfs();int main(){ int cases; scanf("%d",&cases); while(cases--) { scanf("%d%d",&n,&m); for(i=0;i<n;i++) for(j=0;j<m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) //找出起点 { begin.x=i; begin.y=j; begin.time=6; begin.step=0; } mark[i][j] = 0 ;//标记初始化为0 } bfs(); } return 0;}void bfs(){ queue<node>q; q.push(begin); //起点入队 mark[begin.x][begin.y] = begin.time ; node p,temp; while(!q.empty()) //队列为空还没找到就是-1了 { p=q.front(); q.pop(); for(i=0;i<4;i++) { temp=p; temp.x+=dir[i][0]; temp.y+=dir[i][1]; if(temp.x>n||temp.x<0||temp.y>m||temp.y<0||map[temp.x][temp.y]==0) //边界判断 continue; temp.step++; //步数加1 temp.time--; //剩余时间减1 if(map[temp.x][temp.y]==3) //出口 { printf("%d\n",temp.step); return ; //退出函数 } else if ( map[temp.x][temp.y] == 4 ) //还原点 { temp.time = 6 ; } if( temp.time > 1 && mark[temp.x][temp.y] < temp.time ) //标记该点当剩余时间更大时就标记 { mark[temp.x][temp.y] = temp.time; q.push(temp) ; } } } printf("%d\n",-1);//队列为空还没找到就是-1了}
0 0
- HDU 1072(BFS)
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- hdu 1072 Nightmare BFS
- HDU 1072 BFS
- hdu 1072 Nightmare bfs
- hdu 1072 BFS
- HDU 1072 BFS
- Hdu 1072 Nightmare[Bfs]
- HDU 1072(bfs)
- hdu 1072 Nightmare--bfs
- hdu 1072bfs
- HDU 1072 Nightmare(BFS)
- HDU 1072 Nightmare (BFS)
- HDU 1072 Nightmare (BFS)
- bfs-hdu-1072-Nightmare
- HDU 1072 Nightmare【BFS】
- HDU 1072 Nightmare BFS
- cocos2d-x 通过JNI实现c/c++和Android的java层函数互调
- Timus 1013 DP
- OpenSSL库验证PKCS7签名
- 黑马程序员学习log第六篇基础知识:JAVA的面向对象之IO总结
- xml The processing instruction target matching "[xX][mM][lL]" is not allowed
- HDU 1072(bfs)
- IOS多线程开发之GCD
- 图解排序算法及C语言实现之 ------ 快速排序:Quick Sort
- Makefile学习(二)
- Openssl验证PKCS7签名是否有效 .
- 单选按钮 、 下拉框 、 复选框 选中问题
- 干货——mac下配置hudson自动checkout代码编译打包
- 完全免费的中药管理软件
- svnserve.conf:20: Option expected