#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
const int N = 4001;
int a[N][N];
int n, m;
int dx[4]={0, 0, 1, -1};
int dy[4]={1, -1, 0, 0};
bool mark[N][N];
int bfs(int x, int y){
int ans=1;
deque<pair<pair<int, int>, int>> q;
q.push_back({{x, y}, 1});
mark[x][y]=1;
while(!q.empty()){
pair<int, int> xy=q.front().F;
ans=max(ans, q.front().S);
q.pop_front();
x=xy.F;
y=xy.S;
for(int i=0; i< 4; i++){
int nx=x+dx[i];
int ny=y+dy[i];
if(nx>=0 && nx<n && ny>=0 && ny<m && !mark[nx][ny] && a[nx][ny]!=2){
if(a[x][y]==a[nx][ny]){
q.push_front({{nx, ny}, ans});
}
else{
q.push_back({{nx, ny}, ans+1});
}
mark[nx][ny]=1;
}
}
}
return ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i=0; i< n; i++){
for(int j=0; j<m; j++){
char c;
cin >> c;
if(c=='.')a[i][j]=2;
else if(c=='R')a[i][j]=1;
}
}
cout << bfs(0, 0);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
12892 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
1 ms |
2652 KB |
Output is correct |
4 |
Correct |
7 ms |
12892 KB |
Output is correct |
5 |
Correct |
3 ms |
7772 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2652 KB |
Output is correct |
8 |
Correct |
1 ms |
2648 KB |
Output is correct |
9 |
Correct |
1 ms |
4952 KB |
Output is correct |
10 |
Correct |
3 ms |
7452 KB |
Output is correct |
11 |
Correct |
2 ms |
7512 KB |
Output is correct |
12 |
Correct |
6 ms |
7768 KB |
Output is correct |
13 |
Correct |
4 ms |
7772 KB |
Output is correct |
14 |
Correct |
3 ms |
7652 KB |
Output is correct |
15 |
Correct |
11 ms |
12888 KB |
Output is correct |
16 |
Correct |
12 ms |
12888 KB |
Output is correct |
17 |
Correct |
10 ms |
12636 KB |
Output is correct |
18 |
Correct |
7 ms |
12888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
76376 KB |
Output is correct |
2 |
Correct |
44 ms |
29264 KB |
Output is correct |
3 |
Correct |
276 ms |
94604 KB |
Output is correct |
4 |
Correct |
76 ms |
42064 KB |
Output is correct |
5 |
Correct |
156 ms |
70232 KB |
Output is correct |
6 |
Correct |
582 ms |
113896 KB |
Output is correct |
7 |
Correct |
13 ms |
78680 KB |
Output is correct |
8 |
Correct |
13 ms |
76588 KB |
Output is correct |
9 |
Correct |
2 ms |
2652 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
14 ms |
78428 KB |
Output is correct |
12 |
Correct |
2 ms |
5212 KB |
Output is correct |
13 |
Correct |
43 ms |
29268 KB |
Output is correct |
14 |
Correct |
25 ms |
21336 KB |
Output is correct |
15 |
Correct |
26 ms |
23644 KB |
Output is correct |
16 |
Correct |
20 ms |
10844 KB |
Output is correct |
17 |
Correct |
105 ms |
45164 KB |
Output is correct |
18 |
Correct |
87 ms |
44880 KB |
Output is correct |
19 |
Correct |
73 ms |
42068 KB |
Output is correct |
20 |
Correct |
64 ms |
39248 KB |
Output is correct |
21 |
Correct |
164 ms |
72772 KB |
Output is correct |
22 |
Correct |
156 ms |
70224 KB |
Output is correct |
23 |
Correct |
219 ms |
58820 KB |
Output is correct |
24 |
Correct |
164 ms |
72792 KB |
Output is correct |
25 |
Correct |
400 ms |
94292 KB |
Output is correct |
26 |
Correct |
515 ms |
157700 KB |
Output is correct |
27 |
Correct |
459 ms |
121444 KB |
Output is correct |
28 |
Correct |
560 ms |
113872 KB |
Output is correct |
29 |
Correct |
541 ms |
110396 KB |
Output is correct |
30 |
Correct |
515 ms |
119452 KB |
Output is correct |
31 |
Correct |
456 ms |
75112 KB |
Output is correct |
32 |
Correct |
434 ms |
118976 KB |
Output is correct |