#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> animals; //0 - bull; 1 - tiger; 2 - visited
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
int h,w;
int cntanimals(){
bool animal = animals[0][0];
queue<pair<int,int>> qcurr;
queue<pair<int,int>> qnext;
qcurr.push({0,0});
animals[0][0]=2;
int anicnt = 0;
while (qcurr.size()>0){
auto f = qcurr.front();
qcurr.pop();
int x = f.first;
int y = f.second;
//cout<<x<<" "<<y<<endl;
for (int i = 0; i<4; i++){
int nx = x+dx[i];
int ny = y+dy[i];
if (!(0<=nx&&nx<w&&0<=ny&&ny<h)){
continue;
}
if (animals[nx][ny]==animal){
animals[nx][ny]=2;
qcurr.push({nx,ny});
} else if (animals[nx][ny]==!animal){
animals[nx][ny]=2;
qnext.push({nx,ny});
}
}
if (qcurr.size()==0){
//cout<<"===="<<endl;
swap(qnext,qcurr);
animal=!animal;
anicnt++;
}
}
return anicnt;
}
int main(){
cin.tie(0);
ios_base::sync_with_stdio(0);
cin>>h>>w;
animals.resize(w,vector<int>(h,2));
for (int y = 0; y<h; y++){
for (int x = 0; x<w; x++){
char c;
cin>>c;
if (c=='B'){
animals[x][y]=0;
} else if (c=='T'){
animals[x][y]=1;
}
}
}
cout<<cntanimals()<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
5 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
26 ms |
5120 KB |
Output is correct |
17 |
Correct |
23 ms |
4992 KB |
Output is correct |
18 |
Correct |
26 ms |
5248 KB |
Output is correct |
19 |
Correct |
26 ms |
5248 KB |
Output is correct |
20 |
Correct |
23 ms |
5120 KB |
Output is correct |
21 |
Correct |
53 ms |
4864 KB |
Output is correct |
22 |
Correct |
54 ms |
4984 KB |
Output is correct |
23 |
Correct |
56 ms |
4992 KB |
Output is correct |
24 |
Correct |
59 ms |
5120 KB |
Output is correct |
25 |
Correct |
55 ms |
5120 KB |
Output is correct |
26 |
Correct |
54 ms |
5120 KB |
Output is correct |
27 |
Correct |
55 ms |
4864 KB |
Output is correct |
28 |
Correct |
60 ms |
5112 KB |
Output is correct |
29 |
Correct |
54 ms |
5112 KB |
Output is correct |
30 |
Correct |
54 ms |
5120 KB |
Output is correct |