hdu 1072 Nightmare(bfs)
来源:互联网 发布:如何对数据进行预处理 编辑:程序博客网 时间:2024/05/21 18:02
小记:完全拿来练手感,练手速,练自己的bfs逻辑能力的
思路:bfs,输入时记下起点和终点,在bfs判断时,我们往4个方向判的时候就直接判断其时间是否等于0,即如果往旁边走,时间是否等于0,等于的话就不需要再往下了。
然后就是边界判断,且不能是墙。这时我们再看它是否可以reset炸弹的时间,可以就reset,然后我们要对每一点进行标记,防止多做无用功。
标记这里同时也起了标记到达这点剩余最多的时间,如果再到这点来的,而它的剩余时间没有这点历史上剩余的最多的多,那么就没必要入队了。
然后便是将符合要求的全部入队,出队时,判断是否是终点即可。 因为之前判了时间为0的点了,所以这里不会出现到达终点为0的情况。
其实题目的描述挺吓人的,说什么可以在一点reset几次,其实reset一次就行了,如果reset这一次之后还是到达不了终点,你再去reset还是到达不了。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <map>#include <set>#include <vector>#include <stack>#include <queue>#include <algorithm>using namespace std;#define mst(a,b) memset(a,b,sizeof(a))#define REP(a,b,c) for(int a = b; a < c; ++a)#define eps 10e-8const int MAX_ = 60;const int N = 100010;const int INF = 0x7fffffff;struct Point { int x, y; int re; int step;}p2, p1;int dir[4][2] = {{0,-1},{0,1},{-1,0},{1,0}};int vis[MAX_][MAX_];int a[MAX_][MAX_], num[10][10][100];int n, m, k, ti;char str[MAX_][MAX_];int bfs(Point x){ queue<Point> q; mst(vis, 0); x.step = 0; x.re = 6; q.push(x); while(!q.empty()){ Point cur; cur = q.front(); q.pop(); if(cur.x == p2.x && cur.y == p2.y ){ return cur.step; } REP(i, 0, 6){ Point nt; nt.x = cur.x + dir[i][1]; nt.y = cur.y + dir[i][0]; nt.step = cur.step+1; nt.re = cur.re-1; if(nt.re == 0)continue; if((nt.x > -1 && nt.x < n) && (nt.y > -1 && nt.y < m) && a[nt.x][nt.y] ){ if(a[nt.x][nt.y] == 4)nt.re = 6; if(!vis[nt.x][nt.y] || nt.re > vis[nt.x][nt.y]){ vis[nt.x][nt.y] = nt.re; q.push(nt); } } } } return -1;}int main(){int T, cnt;scanf("%d", &T);while(T-- && scanf("%d%d", &n, &m)){ cnt = 0; REP(i, 0, n)REP(j, 0, m){ scanf("%d", &a[i][j]); if(a[i][j] == 2){ p1.x = i; p1.y = j; } else if(a[i][j] == 3){ p2.x = i; p2.y = j; } } int ans; ans = bfs(p1); printf("%d\n",ans);}return 0;}
0 0
- hdu 1072 Nightmare (bfs)
- hdu 1072 Nightmare (BFS)
- hdu 1072 Nightmare bfs()
- hdu 1072Nightmare(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)
- 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
- C#中委托和事件使用教程
- 简单的if语句(经典的分数判断)
- 【学习记录】面向对象的基本概念3
- 黑马程序员——字符串总结
- 像打游戏、踢足球那样热爱编程
- hdu 1072 Nightmare(bfs)
- 安卓开发这件事-1.安卓环境搭建
- Java 之--------------------- 强大的API
- 隐马尔科夫模型(HMM)分词研究
- unity3d学习笔记
- POJ 3345——Bribing FIPA(树形DP)
- C语言基础教程之hello world
- uva 12356 - Army Buddies
- 常用算法(1)