-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1238 - Power Puff Girls
105 lines (91 loc) · 2.02 KB
/
1238 - Power Puff Girls
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <bits/stdc++.h>
#define inf 1<<30
using namespace std;
typedef pair <int, int> pi;
char maze[101][101];
int dist[101][101];
int reach[101][101];
int fx[]= {0,-1, 0,1};
int fy[]= {1, 0,-1,0};
int row ,col;
int bfs_flood_fill(int i , int j)
{
reach[i][j] = 1;
dist[i][j] = 0;
queue<pi> q;
q.push(make_pair(i,j));
while(!q.empty())
{
int a , b;
a = q.front().first;
b = q.front().second;
q.pop();
for(i = 0; i < 4; i++)
{
int x , y;
x = a + fx[i];
y = b + fy[i];
if(x >= 0 and x < row and y >= 0 and y < col and maze[x][y] != 'm' and maze[x][y] != '#'and reach[x][y] == -1)
{
dist[x][y] = min(dist[a][b] + 1 , dist[x][y]);
reach[x][y] = 1;
q.push(make_pair(x,y));
}
}
}
}
void setx ()
{
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j ++)
{
reach[i][j] = -1;
dist[i][j] = inf;
}
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int tes, o =0;
cin >> tes;
while(tes--)
{
o++;
cin >> row >> col;
int x, y , a1 , a2 , b1 , b2, c1, c2;
for(int i = 0; i < row; i++)
{
for(int j = 0; j < col; j++)
{
cin >> maze[i][j];
if(maze[i][j] == 'h')
{
x = i;
y = j;
}
if(maze[i][j] == 'a')
{
a1 = i;
a2 = j;
}
if(maze[i][j] == 'b')
{
b1 = i;
b2 = j;
}
if(maze[i][j] == 'c')
{
c1 = i;
c2 = j;
}
}
}
setx();
bfs_flood_fill(x,y);
int mx = max(max(dist[a1][a2], dist[b1][b2]) , dist[c1][c2]);
cout<<"Case "<< o << ": " << mx << endl;
}
}