-
Notifications
You must be signed in to change notification settings - Fork 0
/
Endgame.cpp
50 lines (47 loc) · 1.07 KB
/
Endgame.cpp
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
#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1001;
const int dx[] = {0, 0, 1, -1};
const int dy[] = {1, -1, 0, 0};
int n, m, ans = 0, d = 0;
char table[MAX_N][MAX_N];
queue<tuple<int, int, int>> q;
void bfs()
{
while (!q.empty())
{
auto [x, y, s] = q.front(); // C++17: structured binding declaration
q.pop();
if (x < 0 || n <= x || y < 0 || m <= y)
continue;
if (table[x][y] == 'C')
continue;
if (table[x][y] == 'T')
{
ans = max(ans, s);
d--;
}
table[x][y] = 'C';
for (int i = 0; i < 4; i++)
q.push({x + dx[i], y + dy[i], s + 1});
}
}
int main()
{
cin >> n >> m;
for (int i = 0; i < n; i++)
scanf("%s", table[i]);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (table[i][j] == 'I')
q.push({i, j, 0});
else if (table[i][j] == 'T')
d++;
}
}
bfs();
printf("%d\n", d != 0 ? -1 : ans);
return 0;
}