#include<bits/stdc++.h>
#define a first
#define b second
using namespace std;
typedef pair<int, int>PII;
const int MAXL = 4000;
int n, m;
vector<string>graph;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
int main()
{
int ans = 1; // 至少有一个animal
iostream :: sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
graph.resize(n);
for(int i = 0; i < n; i ++)
cin >> graph[i];
// 0-1 bfs 0 -> push_front, 1 -> push_back() --> increasing deque
// q 存储位置
// dist 存储到 start 的距离
deque<PII>q;
q.push_back({0, 0});
vector<vector<int>>dist(n, vector<int>(m, INT_MAX));
dist[0][0] = 1; // 根的depth是1
while(q.size())
{
auto t = q.front();
q.pop_front();
ans = max(ans, dist[t.a][t.b]);//已可用它更新其它点的dist是否最大
// 字母走方向的图不是adj的图
for(int i = 0; i < 4; i ++)
{
int x = t.a + dx[i], y = t.b + dy[i];
if(x < 0 || x >= n || y < 0 || y >= m)continue;
if((graph[x][y] == '.' ) || dist[x][y] != INT_MAX)continue;// un-visited
// 在同一node内,weight为0
if(graph[x][y] == graph[t.a][t.b])
{
q.push_front({x, y});
dist[x][y] = dist[t.a][t.b];
}
else
{
q.push_back({x, y});
dist[x][y] = dist[t.a][t.b] + 1;
}
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
1624 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
3 ms |
1372 KB |
Output is correct |
5 |
Correct |
1 ms |
860 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
604 KB |
Output is correct |
12 |
Correct |
3 ms |
860 KB |
Output is correct |
13 |
Correct |
1 ms |
860 KB |
Output is correct |
14 |
Correct |
1 ms |
860 KB |
Output is correct |
15 |
Correct |
5 ms |
1628 KB |
Output is correct |
16 |
Correct |
7 ms |
1624 KB |
Output is correct |
17 |
Correct |
4 ms |
1628 KB |
Output is correct |
18 |
Correct |
3 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
856 KB |
Output is correct |
2 |
Correct |
22 ms |
8284 KB |
Output is correct |
3 |
Correct |
108 ms |
80976 KB |
Output is correct |
4 |
Correct |
33 ms |
19032 KB |
Output is correct |
5 |
Correct |
79 ms |
45400 KB |
Output is correct |
6 |
Correct |
373 ms |
95664 KB |
Output is correct |
7 |
Correct |
1 ms |
860 KB |
Output is correct |
8 |
Correct |
1 ms |
860 KB |
Output is correct |
9 |
Correct |
1 ms |
604 KB |
Output is correct |
10 |
Correct |
0 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
860 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
20 ms |
8308 KB |
Output is correct |
14 |
Correct |
12 ms |
5008 KB |
Output is correct |
15 |
Correct |
7 ms |
5464 KB |
Output is correct |
16 |
Correct |
11 ms |
3676 KB |
Output is correct |
17 |
Correct |
55 ms |
20740 KB |
Output is correct |
18 |
Correct |
32 ms |
20312 KB |
Output is correct |
19 |
Correct |
36 ms |
19196 KB |
Output is correct |
20 |
Correct |
26 ms |
17500 KB |
Output is correct |
21 |
Correct |
67 ms |
46940 KB |
Output is correct |
22 |
Correct |
81 ms |
45404 KB |
Output is correct |
23 |
Correct |
112 ms |
39256 KB |
Output is correct |
24 |
Correct |
65 ms |
45912 KB |
Output is correct |
25 |
Correct |
155 ms |
81028 KB |
Output is correct |
26 |
Correct |
177 ms |
112180 KB |
Output is correct |
27 |
Correct |
255 ms |
110344 KB |
Output is correct |
28 |
Correct |
393 ms |
95600 KB |
Output is correct |
29 |
Correct |
355 ms |
91568 KB |
Output is correct |
30 |
Correct |
331 ms |
99316 KB |
Output is correct |
31 |
Correct |
272 ms |
52248 KB |
Output is correct |
32 |
Correct |
249 ms |
100552 KB |
Output is correct |