Hdu 1072 Nightmare[Bfs]
来源:互联网 发布:2016年全年经济数据 编辑:程序博客网 时间:2024/05/10 13:10
题目连接:点击打开链接
迷宫类型的bfs。
需要注意的是:一个格可能会访问两次。
能够访问两次的标准的:再次访问到该点时time大了。
代码:
#include<cstdio>#include<cstring>#include<queue>#include<algorithm>using namespace std;const int N=10;int map[N][N];int t[N][N];int n,m,startx,starty,endx,endy;int dx[]={0,0,1,-1};int dy[]={1,-1,0,0};struct Node{ int x,y; int time; int step; Node(){} Node(int x1,int y1,int t,int s):x(x1),y(y1),time(t),step(s) {}};bool operator <(Node a,Node b){ return a.step>b.step;}void bfs(){ memset(t,0,sizeof(t)); Node node(startx,starty,6,0); t[startx][starty]=6; priority_queue<Node> q; q.push(node); while(!q.empty()) { Node temp,k; temp=q.top(); q.pop(); if(temp.x==endx&&temp.y==endy)//这个条件可以改的. { printf("%d\n",temp.step); return ; } for(int i=0;i<4;i++) { k.x=temp.x+dx[i];k.y=temp.y+dy[i]; if(k.x>=1&&k.x<=n&&k.y>=1&&k.y<=m&&map[k.x][k.y]!=0) { if(map[k.x][k.y]==4&&temp.time>1) k.time=6; else k.time=temp.time-1; if(k.time>t[k.x][k.y]) { k.step=temp.step+1; q.push(k); t[k.x][k.y]=k.time; } } } } printf("-1\n");}int main(){// freopen("1.txt","r",stdin); int T; scanf("%d",&T); while(T--) { scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) { scanf("%d",&map[i][j]); if(map[i][j]==2) startx=i,starty=j; if(map[i][j]==3) endx=i,endy=j; } bfs(); }}
- BFS hdu 1072 Nightmare
- HDU 1072 Nightmare BFS
- hdu 1072 Nightmare BFS
- hdu 1072 Nightmare bfs
- Hdu 1072 Nightmare[Bfs]
- hdu 1072 Nightmare--bfs
- HDU 1072 Nightmare(BFS)
- HDU 1072 Nightmare (BFS)
- HDU 1072 Nightmare (BFS)
- bfs-hdu-1072-Nightmare
- HDU 1072 Nightmare【BFS】
- HDU 1072 Nightmare BFS
- HDU 1072 Nightmare BFS
- hdu 1072 Nightmare (bfs)
- hdu 1072 Nightmare (BFS)
- HDOJ/HDU 1072 Nightmare (bfs)
- hdu 1072 Nightmare bfs()
- HDU 1072 Nightmare 限制BFS
- bootstrap中的popovers
- C#WebBrowser控件使用教程与技巧收集
- MinGW介绍及安装和配置
- bootstrap中的tooltip
- Mysql explain用法和性能分析
- Hdu 1072 Nightmare[Bfs]
- ACM-DP之Common Subsequence——HDU1159
- Django 学习笔记(七)
- Mysql导出表结构及表数据的方法
- 回表
- MySql 给表添加一个联合唯一索引
- LoaderCallbacks<RESTLoader.RESTResponse> 自动重新调用
- 黑客常用工具
- python 操作mysql