我是靠谱客的博主 懦弱巨人,这篇文章主要介绍ZOJ1649,现在分享给大家,希望可以做个参考。

一个迷宫,许多个r,主人公在a,坏人在x,走到坏人的格子里需要打死坏人

我能r们最少多少时间找到a

题解:倒过来a分身找r就好,判重用到x,y的最短时间

复制代码
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
#include<bits/stdc++.h> #define X first #define Y second using namespace std; int n,m; typedef pair<int,int> pii; typedef pair<pii,int> piii; const int movx[4]={0,1,0,-1}; const int movy[4]={1,0,-1,0}; void Gao() { char a[300][300]; int f[300][300]; for (int i=0;i<n;i++) scanf("%s",a[i]); for (int i=0;i<n;i++) for (int j=0;j<m;j++) f[i][j]=3276700; int stx,sty; for (int i=0;i<n;i++) for (int j=0;j<m;j++) if (a[i][j]=='a') stx=i,sty=j; f[stx][sty]=0; queue<piii> q; q.push(make_pair(make_pair(stx,sty),0)); int ans=3276700; while(!q.empty()) { int tmx=q.front().X.X,tmy=q.front().X.Y,tmstp=q.front().Y; q.pop(); if (a[tmx][tmy]=='r') { ans=min(ans,tmstp); continue; } for (int i=0;i<4;i++) { int xx=tmx+movx[i]; int yy=tmy+movy[i]; if (xx>=n ||xx<0||yy>=m||yy<0)continue; if (a[xx][yy]=='#') continue; int pp=tmstp+(a[xx][yy]=='x'?2:1); if (pp<f[xx][yy]) { q.push(make_pair(make_pair(xx,yy),pp)); f[xx][yy]=pp; } } } if (ans==3276700) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl; else cout<<ans<<endl; } int main() { while (cin>>n>>m) Gao(); return 0; }


最后

以上就是懦弱巨人最近收集整理的关于ZOJ1649的全部内容,更多相关ZOJ1649内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部