zoj 1649 Rescue
2015-11-04 21:37
435 查看
题目链接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=98233#problem/A题目大意:
Angle 的朋友想去营救Angle,其任务是到Angle的位置,Angle 的朋友向上,下,左,右移动一步用时1个单位时间,杀死警卫需要用1个时间。计算Angle的朋友营救Angle至少需要多长的时间。“a” 代表Angle的位置。
“.”代表道路。
“r”代表Angle的朋友。
“#”代表墙。
“x”代表警卫
大体思路:
简单的bfs,与之前直接用bool vis[M] 不同,这次因为可能出现花费时间为1的点,也可能会出现花费时间为2的点,所以用一个time
[M]数组作为标记,当time[cury][curx] (当前位置的时间)大于cost(当前位置需要花费的时间)与上一个位置的时间时,表明time[cury][curx]当前位置的时间可以更新。如果小于,则代表不需要要更新,不用将该点加入到队列中。
代码:
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<iostream> #include<cstring> #include<queue> using namespace std; #define MAX 100000 int N, M; char map[205][205]; int Time[205][205]; int x[] = {-1,1,0,0}; int y[] = {0,0,-1,1}; int ax, ay, rx, ry; void BFS(int ry,int rx) { queue<int> qx; queue<int> qy; qx.push(rx); qy.push(ry); Time[ry][rx] = 0; while (!qx.empty() && !qy.empty()) { int x1 = qx.front(); int y1 = qy.front(); qx.pop(); qy.pop(); for (int i = 0; i < 4; i++) { int x2 = x1 + x[i]; int y2 = y1 + y[i]; int cost=0; if (x2 >= 0 && x2 < M && y2 >= 0 && y2 < N && map[y2][x2]!='#') { if (map[y2][x2] == 'a') { cost = 1; } if (map[y2][x2] == '.') { cost = 1; } else if (map[y2][x2] == 'x') { cost = 2; } if (Time[y2][x2]>cost + Time[y1][x1]) { Time[y2][x2] = cost + Time[y1][x1]; qx.push(x2); qy.push(y2); } } } } } int main() { while (scanf("%d%d", &N, &M) != EOF) { memset(map,0,sizeof(map)); for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { Time[i][j] = MAX; } } for (int i = 0; i < N; i++) { for (int j = 0; j < M; j++) { cin >> map[i][j]; if (map[i][j] == 'a') { ax = j; ay = i; } if (map[i][j] == 'r') { rx = j; ry = i; } } } BFS(ry,rx); if (Time[ay][ax] == MAX) { cout << "Poor ANGEL has to stay in the prison all his life." << endl; } else { printf("%d\n", Time[ay][ax]); } } return 0; }
相关文章推荐
- 开机出现:grub rescue的修复方法
- grub rescue的使用
- grub rescue的修复方法
- zoj3549 快速幂
- Surrounded Regions
- Word Ladder, Gray Code
- UVA 11624
- HDU1495
- HDU2612 Find a way
- HDU1241 Oil Deposits
- Hdu2444二分图
- 最少步数BFS
- ZOJ 1002
- 转v_JULY_v的 BFS和DFS优先搜索算法
- 2015 寒假搜索专题 I - Meteor Shower(BFS)
- ZOJ-3861 DFS+回溯
- zoj_1004-Anagrams by stack
- zoj_2278-Fight for Food
- Same Tree
- E - Roads in the North