#include <iostream>
#include <vector>
#include <algorithm>
#include <utility>
using namespace std;
const int MAXL = 1005;
int h,w;
char track[MAXL][MAXL];
bool check[MAXL][MAXL];
vector<pair<int,int>> p={{1,1}},p2;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> h >> w;
for(int i=1;i<=h;i++){
string a;
cin >> a;
for(int j=0;j<w;j++){
track[i][j+1] = a[j];
}
}
char st=track[1][1];
check[1][1] = true;
for(int i=0;i<p.size();i++){
int x=p[i].first,y=p[i].second;
if(track[x-1][y]==st and not check[x-1][y]){
p.push_back({x-1,y});
check[x-1][y] = true;
}
if(track[x+1][y]==st and not check[x+1][y]){
p.push_back({x+1,y});
check[x+1][y] = true;
}
if(track[x][y-1]==st and not check[x][y-1]){
p.push_back({x,y-1});
check[x][y-1] = true;
}
if(track[x][y+1]==st and not check[x][y+1]){
p.push_back({x,y+1});
check[x][y+1] = true;
}
}
int cnt=0;
while(p.size()>0){
if(st=='T'){
st = 'B';
}
else{
st = 'T';
}
p2 = {};
for(int i=0;i<p.size();i++){
int x=p[i].first,y=p[i].second;
if(track[x-1][y]==st and not check[x-1][y]){
p2.push_back({x-1,y});
check[x-1][y] = true;
}
if(track[x+1][y]==st and not check[x+1][y]){
p2.push_back({x+1,y});
check[x+1][y] = true;
}
if(track[x][y-1]==st and not check[x][y-1]){
p2.push_back({x,y-1});
check[x][y-1] = true;
}
if(track[x][y+1]==st and not check[x][y+1]){
p2.push_back({x,y+1});
check[x][y+1] = true;
}
}
p = p2;
for(int i=0;i<p.size();i++){
int x=p[i].first,y=p[i].second;
if(track[x-1][y]==st and not check[x-1][y]){
p.push_back({x-1,y});
check[x-1][y] = true;
}
if(track[x+1][y]==st and not check[x+1][y]){
p.push_back({x+1,y});
check[x+1][y] = true;
}
if(track[x][y-1]==st and not check[x][y-1]){
p.push_back({x,y-1});
check[x][y-1] = true;
}
if(track[x][y+1]==st and not check[x][y+1]){
p.push_back({x,y+1});
check[x][y+1] = true;
}
}
cnt++;
}
cout << cnt;
return 0;
}
Compilation message
zoo.cpp: In function 'int main()':
zoo.cpp:25:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
25 | for(int i=0;i<p.size();i++){
| ~^~~~~~~~~
zoo.cpp:53:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
53 | for(int i=0;i<p.size();i++){
| ~^~~~~~~~~
zoo.cpp:73:16: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
73 | for(int i=0;i<p.size();i++){
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
416 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
468 KB |
Output is correct |
6 |
Correct |
1 ms |
464 KB |
Output is correct |
7 |
Correct |
1 ms |
468 KB |
Output is correct |
8 |
Correct |
1 ms |
468 KB |
Output is correct |
9 |
Correct |
1 ms |
468 KB |
Output is correct |
10 |
Correct |
1 ms |
468 KB |
Output is correct |
11 |
Correct |
1 ms |
468 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
416 KB |
Output is correct |
14 |
Correct |
1 ms |
460 KB |
Output is correct |
15 |
Correct |
1 ms |
460 KB |
Output is correct |
16 |
Correct |
7 ms |
3780 KB |
Output is correct |
17 |
Correct |
7 ms |
3856 KB |
Output is correct |
18 |
Correct |
7 ms |
3892 KB |
Output is correct |
19 |
Correct |
8 ms |
3796 KB |
Output is correct |
20 |
Correct |
8 ms |
3796 KB |
Output is correct |
21 |
Correct |
35 ms |
3172 KB |
Output is correct |
22 |
Correct |
36 ms |
3272 KB |
Output is correct |
23 |
Correct |
35 ms |
3388 KB |
Output is correct |
24 |
Correct |
36 ms |
3380 KB |
Output is correct |
25 |
Correct |
36 ms |
3288 KB |
Output is correct |
26 |
Correct |
36 ms |
3252 KB |
Output is correct |
27 |
Correct |
34 ms |
3272 KB |
Output is correct |
28 |
Correct |
36 ms |
3156 KB |
Output is correct |
29 |
Correct |
38 ms |
3384 KB |
Output is correct |
30 |
Correct |
38 ms |
3292 KB |
Output is correct |