我是靠谱客的博主 虚心冬日,这篇文章主要介绍Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS),现在分享给大家,希望可以做个参考。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1242

题意:朋友(r)杀敌人(x)救天使(a),时间最短型走迷宫,走格子耗时1s,杀敌人额外耗时1s。

思路:与步数最少的走迷宫不同,时间最少要使用优先队列,时间小的优先出队(据说HDU普通队列也能过……)

有可能会有多个朋友,所以从a开始搜r比较简单。

网上看到另一种方法,就是把杀护卫和走到护卫那个格子看成两个动作等于说是入队两次。


昨天看别人的解题报告刚刚学会的用法(其实这题没必要……):

复制代码
1
2
char g[205][205]; memset(g,'#',sizeof(g));

前几天才知道这类BFS或者DFS还有一个名字:Flood fill 算法……


Zoj的测试数据比较强。

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream> #include <queue> #include <cstdio> #include <string> #include <cstring> using namespace std; char g[205][205]; bool visit[205][205]; int n,m; int dx[4] = {1,-1,0,0}; int dy[4] = {0,0,1,-1}; struct Node { int x,y,time; bool operator < (Node b) const { return time>b.time; } }temp,cur,s; bool OK (Node a) { if (visit[a.x][a.y]==false && g[a.x][a.y]!='#') return true; return false; } int BFS () { int cnt=0; priority_queue<Node> q; q.push(s); while (!q.empty()) { cur = q.top(); q.pop(); if (g[cur.x][cur.y] == 'r') { cnt = cur.time; break; } for (int i=0;i<4;i++) { temp.x=cur.x+dx[i]; temp.y=cur.y+dy[i]; temp.time=cur.time+1; if (OK(temp)) { visit[temp.x][temp.y]=true; if (g[temp.x][temp.y]=='x') temp.time+=1; q.push(temp); } } } return cnt; } int main () { while (~scanf("%d%d",&n,&m)) { memset(g,'#',sizeof(g)); memset(visit,false,sizeof(visit)); for (int i=1;i<=n;i++) { scanf("%s",&g[i][1]); g[i][m+1]='#'; for (int j=1;j<=m;j++) if (g[i][j]=='a') { s.x=i; s.y=j; s.time=0; visit[s.x][s.y]=true; } } int ans=BFS(); if (ans) printf("%dn",ans); else printf("Poor ANGEL has to stay in the prison all his life.n"); } return 0; }


最后

以上就是虚心冬日最近收集整理的关于Hdu 1242 & Zoj 1649 Rescue (优先队列+BFS)的全部内容,更多相关Hdu内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(72)

评论列表共有 0 条评论

立即
投稿
返回
顶部