#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int mod = 1000000007;
int n,m;
#define pb push_back
#define mp make_pair
vector<vector<pair<int,int>>> adj;
void solve()
{
cin >> n >>m ;
vector<string> snow(n);
for(int i = 0;i<n;i++)cin >> snow[i];
deque<pair<int,int>> q;
vector<vector<int>> dist(n,vector<int>(m,0));
vector<vector<int>> dir = {{1,0},{-1,0},{0,1},{0,-1}};
q.push_back({0,0});
dist[0][0] = 1;
int ans = 1;
while(!q.empty())
{
auto curr = q.front();
q.pop_front();
int x = curr.first;
int y = curr.second;
ans = max(ans,dist[x][y]);
for(auto d : dir)
{
int nx = x + d[0];
int ny = y + d[1];
if(nx < 0 || nx >= n || ny < 0 || ny >= m
||snow[nx][ny]=='.' ||dist[nx][ny]!=0)continue;
if (snow[x][y] == snow[nx][ny]) {
dist[nx][ny] = dist[x][y];
q.push_front({nx, ny});
} else {
dist[nx][ny] = dist[x][y] + 1;
q.push_back({nx, ny});
}
}
}
cout << ans << "\n";
return;
}
int main()
{
int tests = 1;
//cin >> tests;
int i = 1;
while (i <= tests)
{
//cout << "Case #"<< i<< ": ";
solve();
if(i!=tests)cout << "\n";
i++;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
1880 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
14 ms |
1440 KB |
Output is correct |
5 |
Correct |
6 ms |
860 KB |
Output is correct |
6 |
Correct |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
5 ms |
604 KB |
Output is correct |
11 |
Correct |
6 ms |
604 KB |
Output is correct |
12 |
Correct |
9 ms |
860 KB |
Output is correct |
13 |
Correct |
4 ms |
860 KB |
Output is correct |
14 |
Correct |
6 ms |
860 KB |
Output is correct |
15 |
Correct |
21 ms |
1884 KB |
Output is correct |
16 |
Correct |
29 ms |
1880 KB |
Output is correct |
17 |
Correct |
23 ms |
1628 KB |
Output is correct |
18 |
Correct |
24 ms |
1372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
856 KB |
Output is correct |
2 |
Correct |
85 ms |
8992 KB |
Output is correct |
3 |
Correct |
574 ms |
93272 KB |
Output is correct |
4 |
Correct |
133 ms |
22352 KB |
Output is correct |
5 |
Correct |
504 ms |
47180 KB |
Output is correct |
6 |
Correct |
1747 ms |
107000 KB |
Output is correct |
7 |
Correct |
3 ms |
860 KB |
Output is correct |
8 |
Correct |
3 ms |
860 KB |
Output is correct |
9 |
Correct |
6 ms |
604 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
2 ms |
860 KB |
Output is correct |
12 |
Correct |
2 ms |
344 KB |
Output is correct |
13 |
Correct |
108 ms |
8988 KB |
Output is correct |
14 |
Correct |
50 ms |
5724 KB |
Output is correct |
15 |
Correct |
41 ms |
6236 KB |
Output is correct |
16 |
Correct |
52 ms |
3676 KB |
Output is correct |
17 |
Correct |
231 ms |
24036 KB |
Output is correct |
18 |
Correct |
195 ms |
23504 KB |
Output is correct |
19 |
Correct |
165 ms |
22336 KB |
Output is correct |
20 |
Correct |
156 ms |
20628 KB |
Output is correct |
21 |
Correct |
364 ms |
48624 KB |
Output is correct |
22 |
Correct |
470 ms |
47180 KB |
Output is correct |
23 |
Correct |
481 ms |
40348 KB |
Output is correct |
24 |
Correct |
455 ms |
47972 KB |
Output is correct |
25 |
Correct |
873 ms |
93508 KB |
Output is correct |
26 |
Correct |
1107 ms |
111992 KB |
Output is correct |
27 |
Correct |
1339 ms |
119968 KB |
Output is correct |
28 |
Correct |
1648 ms |
106968 KB |
Output is correct |
29 |
Correct |
1542 ms |
105456 KB |
Output is correct |
30 |
Correct |
1591 ms |
109380 KB |
Output is correct |
31 |
Correct |
1252 ms |
53244 KB |
Output is correct |
32 |
Correct |
1254 ms |
108800 KB |
Output is correct |