#include <iostream>
#include <vector>
#include <map>
#include <queue>
#include <algorithm>
#include<stack>
#include <set>
using namespace std;
using ll = long long;
const int INF = 1e9;
const int MAXN = 4e3+111;
const int MOD = 1e9 + 7;
const int MAXS = 250*1000+123;
int n, m;
char t[MAXN][MAXN];
bool vis[MAXN][MAXN];
int dist[MAXN][MAXN];
int ans = 1;
pair<int, int> dd[]{ {0,1},{0,-1},{1,0},{-1,0} };
bool isIn(int i, int j)
{
if (i < n && j < m && i >= 0 && j >= 0 && t[i][j] != '.')
return true;
else
return false;
}
void bfs(int i, int j)
{
deque<pair<int,int>> q;
q.push_back({ i,j });
dist[i][j] = 1;
while (!q.empty())
{
pair<int,int> s = q.front();
q.pop_front();
ans = max(ans, dist[s.first][s.second]);
for (pair<int, int> d : dd)
{
int newI = s.first + d.first;
int newJ = s.second + d.second;
if (isIn(newI, newJ) && dist[newI][newJ] == 0)
{
int w = (t[s.first][s.second] != t[newI][newJ]);
dist[newI][newJ] = dist[s.first][s.second] + w;
if (w == 1)
q.push_back({ newI,newJ });
else
q.push_front({ newI,newJ });
}
}
}
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
cin >> n >> m;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
cin >> t[i][j];
bfs(0, 0);
cout << ans << endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
5188 KB |
Output is correct |
2 |
Correct |
1 ms |
460 KB |
Output is correct |
3 |
Correct |
1 ms |
716 KB |
Output is correct |
4 |
Correct |
8 ms |
5048 KB |
Output is correct |
5 |
Correct |
4 ms |
2892 KB |
Output is correct |
6 |
Correct |
1 ms |
460 KB |
Output is correct |
7 |
Correct |
1 ms |
716 KB |
Output is correct |
8 |
Correct |
1 ms |
716 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
3 ms |
2380 KB |
Output is correct |
11 |
Correct |
3 ms |
2124 KB |
Output is correct |
12 |
Correct |
5 ms |
2892 KB |
Output is correct |
13 |
Correct |
4 ms |
2892 KB |
Output is correct |
14 |
Correct |
4 ms |
2892 KB |
Output is correct |
15 |
Correct |
13 ms |
5036 KB |
Output is correct |
16 |
Correct |
14 ms |
5144 KB |
Output is correct |
17 |
Correct |
12 ms |
5056 KB |
Output is correct |
18 |
Correct |
11 ms |
5024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
31180 KB |
Output is correct |
2 |
Correct |
52 ms |
13636 KB |
Output is correct |
3 |
Correct |
347 ms |
58744 KB |
Output is correct |
4 |
Correct |
101 ms |
29112 KB |
Output is correct |
5 |
Correct |
225 ms |
45128 KB |
Output is correct |
6 |
Correct |
698 ms |
93928 KB |
Output is correct |
7 |
Correct |
17 ms |
32504 KB |
Output is correct |
8 |
Correct |
17 ms |
31124 KB |
Output is correct |
9 |
Correct |
2 ms |
588 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
18 ms |
31948 KB |
Output is correct |
12 |
Correct |
1 ms |
1612 KB |
Output is correct |
13 |
Correct |
57 ms |
13600 KB |
Output is correct |
14 |
Correct |
29 ms |
9676 KB |
Output is correct |
15 |
Correct |
30 ms |
12128 KB |
Output is correct |
16 |
Correct |
23 ms |
5076 KB |
Output is correct |
17 |
Correct |
140 ms |
24956 KB |
Output is correct |
18 |
Correct |
115 ms |
31980 KB |
Output is correct |
19 |
Correct |
109 ms |
29200 KB |
Output is correct |
20 |
Correct |
76 ms |
22552 KB |
Output is correct |
21 |
Correct |
201 ms |
43716 KB |
Output is correct |
22 |
Correct |
235 ms |
45044 KB |
Output is correct |
23 |
Correct |
239 ms |
36280 KB |
Output is correct |
24 |
Correct |
198 ms |
41180 KB |
Output is correct |
25 |
Correct |
485 ms |
80788 KB |
Output is correct |
26 |
Correct |
598 ms |
120908 KB |
Output is correct |
27 |
Correct |
657 ms |
98940 KB |
Output is correct |
28 |
Correct |
695 ms |
93964 KB |
Output is correct |
29 |
Correct |
652 ms |
91656 KB |
Output is correct |
30 |
Correct |
655 ms |
96500 KB |
Output is correct |
31 |
Correct |
535 ms |
65084 KB |
Output is correct |
32 |
Correct |
596 ms |
96848 KB |
Output is correct |