Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 38663 Accepted Submission(s): 13362
1 //Author:LanceYu
2 #include<iostream>
3 #include<string>
4 #include<cstring>
5 #include<cstdio>
6 #include<fstream>
7 #include<iosfwd>
8 #include<sstream>
9 #include<fstream>
10 #include<cwchar>
11 #include<iomanip>
12 #include<ostream>
13 #include<vector>
14 #include<cstdlib>
15 #include<queue>
16 #include<set>
17 #include<ctime>
18 #include<algorithm>
19 #include<complex>
20 #include<cmath>
21 #include<valarray>
22 #include<bitset>
23 #include<iterator>
24 #define ll long long
25 using namespace std;
26 const double clf=1e-8;
27 //const double e=2.718281828;
28 const double PI=3.141592653589793;
29 const int MMAX=2147483647;
30 //priority_queue<int>p;
31 //priority_queue<int,vector<int>,greater<int> >pq;
32 int Min,n,m,dir[4][2]={{-1,0},{1,0},{0,-1},{0,1}};//四个方向
33 char map[201][201];
34 struct node
35 {
36 int x,y,step;
37 };
38 int bfs(int x,int y,int x1,int y1)
39 {
40 Min=MMAX;
41 int i;
42 queue<node> q;
43 q.push(node{x,y,0});
44 map[x][y]=‘#‘;
45 while(!q.empty())
46 {
47 node t=q.front();
48 q.pop();
49 if(t.x==x1&&t.y==y1)
50 return t.step;
51 if(map[t.x][t.y]==‘X‘)//当发现之前是守卫的点,标记为不可走,步数+1,直接走到下一步
52 {
53 map[t.x][t.y]=‘#‘;
54 t.step++;
55 q.push(node{t.x,t.y,t.step});
56 continue;
57 }
58 for(i=0;i<4;i++)
59 {
60 int dx=t.x+dir[i][0];
61 int dy=t.y+dir[i][1];
62 if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]==‘a‘)//如果是终点或者正常路径,则标记为不可读,入列
63 {
64 map[dx][dy]=‘#‘;
65 q.push(node{dx,dy,t.step+1});
66 }
67 if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]==‘.‘)
68 {
69 map[dx][dy]=‘#‘;
70 q.push(node{dx,dy,t.step+1});
71 if(dx>=0&&dy>=0&&dx<m&&dy<n&&map[dx][dy]==‘x‘)//用X作为标记,使其优先级移到队列最后
72 {
73 map[dx][dy]=‘X‘;
74 q.push(node{dx,dy,t.step+1});
75
76 }
77
78 }
79 }
80 }
81 return 0;//找不到返回0
82 }
83 int main()
84 {
85 int a,b,a1,b1;
86 while(scanf("%d%d",&m,&n)!=EOF)
87 {
88 for(int i=0;i<m;i++)
89 {
90 scanf("%s",map[i]);
91 for(int j=0;j<n;j++)
92 {
93 if(map[i][j]==‘r‘)//找起点
94 {
95 a=i;
96 b=j;
97 }
98 if(map[i][j]==‘a‘)//找终点
99 {
100 a1=i;
101 b1=j;
102 }
103 }
104 }
105 int ans=bfs(a,b,a1,b1);
106 if(!ans)//如果ans不等于0就输出ans
107 printf("Poor ANGEL has to stay in the prison all his life.\n");
108 else
109 printf("%d\n",ans);
110 }
111 return 0;
112 }
2018-11-15 23:49:18 Author:LanceYu
原文:https://www.cnblogs.com/lanceyu/p/9966951.html