hdu 1072 Nightmare
来源:互联网 发布:十分钟学会python 编辑:程序博客网 时间:2024/06/08 02:36
本题链接:点击打开链接
题目大意:
输入一个N * M的迷宫,数字2为起点,3为终点,起始时间为6,遇到4时,时间还原为6。若能走出迷宫,输出最小步数,若不能,输出-1。
解题思路:
采用广搜(BFS),先将迷宫存放在map[ ]中,每一步可走的方向有四个,即上,下,左,右。将每一步可走的路线搜索一遍,满足条件的即为最短的。
参考AC代码:
#include<stdio.h>#include<string.h>#include<queue>using namespace std;int map[9][9];int mark[9][9];int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};int T,N,M;struct node{int x;int y;int step;int time;};int bfs(int x,int y){queue <node> q;node now,next;now.x=x;now.y=y;now.step=0;now.time=6;q.push(now);while(!q.empty()){now=q.front();q.pop();if(map[now.x][now.y]==3)return now.step;if(now.time==1)continue;for(int i=0;i<4;i++){int a=now.x+dir[i][0];int b=now.y+dir[i][1];next.step=now.step+1;next.time=now.time-1;next.x=a;next.y=b;if(map[a][b]&&mark[a][b]<next.time&&a>=0&&a<N&&b>=0&&b<M&&next.time>0){if(map[a][b]==4){next.time=6;}mark[a][b]=next.time;q.push(next);}}}return -1;}int main(){scanf("%d",&T);while(T--){int sx,sy;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){sx=i;sy=j;map[i][j]=0;}}memset(mark,0,sizeof(mark));printf("%d\n",bfs(sx,sy));}return 0;}
0 0
- HDU 1072 Nightmare
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- HDU 1072 Nightmare
- hdu 1072 Nightmare BFS
- hdu Nightmare 1072
- Hdu 1072 - Nightmare
- hdu 1072 Nightmare bfs
- HDU-1072:Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare
- HDU-1072-Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare
- Nightmare hdu 1072
- hdu 1072 Nightmare
- hdu 1072 Nightmare
- HDU 1072 Nightmare(搜索)
- 事件处理
- C++第九节:多态、虚函数、抽象类
- 爬虫抓取 第三章
- poj1141Brackets Sequence【区间dp+路径记录】
- 带头尾和动画的下拉刷新RecyclerView
- hdu 1072 Nightmare
- [HDU 1011] Starship Troopers 树形dp
- 普通方法求素数与筛法求素数比较
- leetcode_Search a 2D Matrix II
- 简单性能测试函数模板
- C++ std::tr1::function使用
- 关于 RTOS 的选择 (上)
- 机房收费——组合查询的日期和时间的选择实现
- SDNU 1167.花生采摘【NOIP 2004 普及组】【贪心】【8月6】