#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
char meadow[4004][4004];
int num[4004][4004] = {0};
int main() {
int row, column;
cin >> row >> column;
for(int i=1; i<=row; i++) {
for(int j=1; j<=column; j++) {
cin >> meadow[i][j];
}
}
int animals = 1;
queue<pair<int,int>>q;
stack<pair<int,int>>other;
q.push({1,1});
num[1][1] = 1;
while(!q.empty()) {
int bar = q.front().fi;
int kol = q.front().se;
q.pop();
animals = max(animals, num[bar][kol]);
//atas
if(bar-1>=1 && num[bar-1][kol]==0) {
if(meadow[bar-1][kol] == meadow[bar][kol]) {
q.push({bar-1,kol});
num[bar-1][kol] = num[bar][kol];
} else
if(meadow[bar-1][kol] != '.') {
other.push({bar-1,kol});
num[bar-1][kol] = num[bar][kol]+1;
}
}
//bawah
if(bar+1<=row && num[bar+1][kol]==0) {
if(meadow[bar+1][kol] == meadow[bar][kol]) {
q.push({bar+1,kol});
num[bar+1][kol] = num[bar][kol];
} else
if(meadow[bar+1][kol] != '.') {
other.push({bar+1,kol});
num[bar+1][kol] = num[bar][kol]+1;
}
}
//kiri
if(kol-1>=1 && num[bar][kol-1]==0) {
if(meadow[bar][kol-1] == meadow[bar][kol]) {
q.push({bar,kol-1});
num[bar][kol-1] = num[bar][kol];
} else
if(meadow[bar][kol-1] != '.') {
other.push({bar,kol-1});
num[bar][kol-1] = num[bar][kol]+1;
}
}
//kanan
if(kol+1<=column && num[bar][kol+1]==0) {
if(meadow[bar][kol+1] == meadow[bar][kol]) {
q.push({bar,kol+1});
num[bar][kol+1] = num[bar][kol];
} else
if(meadow[bar][kol+1] != '.') {
other.push({bar,kol+1});
num[bar][kol+1] = num[bar][kol]+1;
}
}
if(q.empty()==true) {
while(!other.empty()) {
q.push(other.top());
other.pop();
}
}
}
cout << animals << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
5332 KB |
Output is correct |
2 |
Correct |
1 ms |
468 KB |
Output is correct |
3 |
Correct |
1 ms |
692 KB |
Output is correct |
4 |
Correct |
10 ms |
5076 KB |
Output is correct |
5 |
Correct |
7 ms |
2900 KB |
Output is correct |
6 |
Correct |
1 ms |
468 KB |
Output is correct |
7 |
Correct |
1 ms |
696 KB |
Output is correct |
8 |
Correct |
1 ms |
724 KB |
Output is correct |
9 |
Correct |
1 ms |
1108 KB |
Output is correct |
10 |
Correct |
4 ms |
2388 KB |
Output is correct |
11 |
Correct |
3 ms |
2132 KB |
Output is correct |
12 |
Correct |
7 ms |
2972 KB |
Output is correct |
13 |
Correct |
5 ms |
2900 KB |
Output is correct |
14 |
Correct |
5 ms |
2940 KB |
Output is correct |
15 |
Correct |
16 ms |
5204 KB |
Output is correct |
16 |
Correct |
17 ms |
5336 KB |
Output is correct |
17 |
Correct |
14 ms |
5260 KB |
Output is correct |
18 |
Correct |
10 ms |
5076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
30804 KB |
Output is correct |
2 |
Correct |
79 ms |
14948 KB |
Output is correct |
3 |
Correct |
618 ms |
73256 KB |
Output is correct |
4 |
Correct |
159 ms |
32612 KB |
Output is correct |
5 |
Correct |
387 ms |
53284 KB |
Output is correct |
6 |
Correct |
1079 ms |
104492 KB |
Output is correct |
7 |
Correct |
15 ms |
32212 KB |
Output is correct |
8 |
Correct |
15 ms |
30716 KB |
Output is correct |
9 |
Correct |
3 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
340 KB |
Output is correct |
11 |
Correct |
15 ms |
31572 KB |
Output is correct |
12 |
Correct |
2 ms |
1620 KB |
Output is correct |
13 |
Correct |
84 ms |
14956 KB |
Output is correct |
14 |
Correct |
45 ms |
10436 KB |
Output is correct |
15 |
Correct |
45 ms |
13032 KB |
Output is correct |
16 |
Correct |
33 ms |
5712 KB |
Output is correct |
17 |
Correct |
202 ms |
28644 KB |
Output is correct |
18 |
Correct |
185 ms |
35624 KB |
Output is correct |
19 |
Correct |
159 ms |
32648 KB |
Output is correct |
20 |
Correct |
137 ms |
25644 KB |
Output is correct |
21 |
Correct |
367 ms |
52660 KB |
Output is correct |
22 |
Correct |
398 ms |
53280 KB |
Output is correct |
23 |
Correct |
396 ms |
43712 KB |
Output is correct |
24 |
Correct |
362 ms |
49748 KB |
Output is correct |
25 |
Correct |
789 ms |
94264 KB |
Output is correct |
26 |
Correct |
640 ms |
80764 KB |
Output is correct |
27 |
Correct |
885 ms |
96112 KB |
Output is correct |
28 |
Correct |
1080 ms |
104496 KB |
Output is correct |
29 |
Correct |
1060 ms |
102752 KB |
Output is correct |
30 |
Correct |
955 ms |
100104 KB |
Output is correct |
31 |
Correct |
745 ms |
73460 KB |
Output is correct |
32 |
Correct |
830 ms |
95028 KB |
Output is correct |