hdoj 1242 宽度优先
来源:互联网 发布:网络教育有哪些学校 编辑:程序博客网 时间:2024/05/16 12:00
题目大意:有一个人关在监狱里,他有很多个朋友要去救他,求这些朋友救到他花费的最短时间,碰到警卫,他们就杀死警卫,要花费两个时间值
解题思路:看到最短消耗时间,想到了bfs,有因为有多个朋友r去解救Angel,所以想到以Angel的位置为起点,bfs求到任意一个朋友的位置花费最小的时间,之前直接就这么用bfs结果错了,忽略了碰到警卫是花费两个时间值,而直接走只花费一个时间值,所以还得改造下。用一个二维数组来保存到达各个位置花费的时间,要是走到当前比这个位置原来花费的时间小,则更新值,才把这个状态加入队列中
#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;struct node{ int x, y, step;};const int maxn = 210;int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int n, m, num, sx, sy, minv;char prison[maxn][maxn];int val[maxn][maxn];bool bfs();int main(){ while(scanf("%d %d", &n, &m) != EOF) { num = 0; memset(val, 0, sizeof(val)); for(int i = 0; i < n; i++) { scanf("%s", prison[i]); for(int j = 0; j < m; j++) { if(prison[i][j] == 'a') { sx = i; sy = j; } val[i][j] = 0x7fffffff; } } if(bfs()) printf("%d\n", minv); else printf("Poor ANGEL has to stay in the prison all his life.\n"); } return 0;}bool bfs(){ queue<node> que; node s; s.x = sx; s.y = sy; s.step = 0; que.push(s); minv = 0x7fffffff; while(!que.empty()) { node tmp = que.front(); que.pop(); if(prison[tmp.x][tmp.y] == 'r') minv = min(minv, tmp.step); for(int i = 0; i < 4; i++) { int dx = tmp.x + dir[i][0]; int dy = tmp.y + dir[i][1]; if(dx >= 0 && dx < n && dy >=0 && dy < m && prison[dx][dy] != '#') { if(val[dx][dy] > tmp.step + 1) { node next; next.x = dx; next.y = dy; next.step = tmp.step + 1; if(prison[dx][dy] == 'x') next.step++; val[dx][dy] = next.step; que.push(next); } } } } if(minv != 0x7fffffff) return true; return false;}
- hdoj 1242 宽度优先
- hdoj 1072 Nightmare 宽度优先遍历
- hdoj 1253 胜利大逃亡 宽度优先遍历
- HDOJ 1242 Rescue (BFS+优先队列)
- HDOJ(1242)BFS+优先队列
- HDOJ 1242 Rescue 优先队列+bfs
- hdoj 1242 Rescue 【BFS + 优先队列】
- HDOJ 1242 Rescue(bfs+优先队列)
- hdoj-1242-Rescue【广搜+优先队列】
- hdoj 1242 Rescue (bfs+优先队列)
- HDOJ-1242(Rescue)(bfs+优先队列)
- hdoj 1242 Rescue (bfs 优先队列)
- HDOJ 1242 Rescue(BFS + 优先队列)
- HDOJ 1242 Rescue(优先队列+BFS)
- HDOJ 1242 Rescue (BFS + 优先队列)
- 宽度优先搜索 BFS
- 宽度优先搜索BFS
- poj3126,宽度优先算法
- Python实现二分查找
- 单例设计模式
- 在.NET/Mono上运行Java:Jeroens Frijters谈IKVM.NET
- 续:Mac狮子系统又暴安全隐患
- 输出1到1000,不使用循环
- hdoj 1242 宽度优先
- PHP session回收机制
- 分析stl function objects模块
- yii 中文书-agile_web_application_development_with_yii1.1_and_php5
- USACO Feed Ratios, Magic Squares
- Levenshtein distance
- linux下取消页读写保护
- Php 体系结构及其执行概述
- Begin~