HDU 1072 Nightmare
来源:互联网 发布:上海人工智能展览会 编辑:程序博客网 时间:2024/06/01 09:41
hdu 1072
题目大意就是一个人身上有一个炸弹,这个炸弹6分钟后爆炸,在迷宫中,0是墙,不能走; 1可以走, 2是开始位置,3是出口,4是炸弹充值装置,如果走到那个位置时,炸弹事件大于0,就会把、时间重置为 6 分钟(这里我开始理解错了,以为他获得了这个装置,可以随时使用,意思就是相当于增加6分钟);走到出口时,剩余的时间必须大于0;
这个题,用BFS,开一个结构体,记录走的步数,还有所用的时间,或者剩余的时间,都行,每走一步,时间减一,走到4,时间置为6,在走,所以可能会走重复的路,所以普通的标记不行,但如果不用标记,会导致内存使用超出限制,所以灵活的标记,每走到这一步,记录下这一步剩余的时间,如果下次走到这个点,时间多了,就放入队列;
代码
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <queue>using namespace std;int map[10][10];int vis[10][10];int movex[4] = {0,0,-1,1};int movey[4] = {-1,1,0,0};int n, m, a, b, c, d;int s, flag;struct point{ int x, y, step, time;} st;queue<point>q;void bfs(point st);int main(){ //freopen("1.txt","w",stdout); int t; int i, j; scanf("%d", &t); while (t--) { scanf("%d %d", &n, &m); memset(map, 0, sizeof(map)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { scanf("%d", &map[i][j]); } } flag = 0; memset(vis, 0, sizeof(vis)); for (i = 0; i < n; i++) { for (j = 0; j < m; j++) { if (map[i][j] == 2) { a = i; b = j; } if (map[i][j] == 3) { c = i; d = j; } } } st.x = a; st.y = b; st.step = 0; st.time = 6; bfs(st); if (flag) { printf("%d\n", s); } else { printf("-1\n"); } }}void bfs(point st){ while (!q.empty()) { q.pop(); } q.push(st); while (!q.empty()) { point now = q.front(); q.pop(); //printf("x = %d,y = %d,step = %d,time = %d,k = %d\n",now.x,now.y,now.step,now.time,now.k); for (int i = 0; i < 4; i++) { int xx = now.x, yy = now.y; xx += movex[i]; yy += movey[i]; if (now.time <= 0) { continue; } if (map[xx][yy] == 3&&now.time>0) { s = now.step + 1; flag = 1; return; } if (xx >= 0 && xx < n && yy >= 0 && yy < m && map[xx][yy] != 0) { point next = now; next.step += 1; next.time -= 1; if(map[xx][yy]==4) { next.time = 6; } if(next.time>1 && vis[xx][yy]<next.time) //这里,时间多了,放入队列 { vis[xx][yy]=next.time; next.x = xx; next.y = yy; q.push(next); } } } }}
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++ Virtual详解
- 最短时间
- Linux 自旋锁
- vb是如何连接数据库的
- hdu 4107卡时线段树
- HDU 1072 Nightmare
- 用yeelink记录树莓派的工作温度
- 程序员生存定律[三] 管理向左,技术向右
- 【索引】Codeforces Round #258 (Div. 2)
- 如何Debug Android 输入装置
- IDOC 相关程序和事务
- U3D杂记:关键字解析
- 安卓换肤功能实现
- NSProcessInfo类方法