#include <bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int row, col;
cin>>row>>col;
char meadow[row][col];
int vis[row][col];
for(int i=0;i<row;i++){
fill(vis[i],vis[i]+col,0);
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cin>>meadow[i][j];
}
}
int numberoftimes=0;
int dx[4] = {0, 0, -1, 1},dy[4] = {1, -1, 0, 0}; //dx and dy stores the way to get to the adjacent nodes
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
if(vis[i][j]==0 and (meadow[i][j]=='F' or meadow[i][j]=='R')){
numberoftimes++;
queue<pair<int, int> > q; //pair contains <y_coord, x_coord>
char currchar=meadow[i][j];
q.push(make_pair(i, j)); //push the starting node
while (!q.empty()) {
int y = q.front().first;
int x = q.front().second;
vis[y][x]=1;
q.pop();
for (int i = 0; i < 4; i++) {
int new_y = y + dy[i], new_x = x + dx[i]; //adjacent nodes
if(new_y>=0 and new_y<row and new_x>=0 and new_x<col and meadow[new_y][new_x]==currchar){
char changedchar;
if(currchar=='F') changedchar='R';
else changedchar='F';
meadow[new_y][new_x]=changedchar;
q.push(make_pair(new_y, new_x));
}
//check if new node is within bounds, not visited and reachable (code for if it is not)
}
}
}
}
}
/*
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<meadow[i][j]<<" ";
}
cout<<"\n";
}
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
cout<<vis[i][j]<<" ";
}
cout<<"\n";
}
*/
cout<<numberoftimes;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
79 ms |
1624 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
4 |
Correct |
11 ms |
1116 KB |
Output is correct |
5 |
Incorrect |
26 ms |
860 KB |
Output isn't correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
10 |
Incorrect |
26 ms |
836 KB |
Output isn't correct |
11 |
Correct |
3 ms |
600 KB |
Output is correct |
12 |
Correct |
25 ms |
960 KB |
Output is correct |
13 |
Incorrect |
24 ms |
856 KB |
Output isn't correct |
14 |
Incorrect |
24 ms |
860 KB |
Output isn't correct |
15 |
Correct |
203 ms |
1904 KB |
Output is correct |
16 |
Correct |
79 ms |
1624 KB |
Output is correct |
17 |
Incorrect |
174 ms |
1852 KB |
Output isn't correct |
18 |
Correct |
9 ms |
1116 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
224 ms |
604 KB |
Output isn't correct |
2 |
Execution timed out |
2101 ms |
9560 KB |
Time limit exceeded |
3 |
Execution timed out |
2009 ms |
94212 KB |
Time limit exceeded |
4 |
Execution timed out |
2082 ms |
22440 KB |
Time limit exceeded |
5 |
Correct |
237 ms |
53072 KB |
Output is correct |
6 |
Execution timed out |
2009 ms |
94312 KB |
Time limit exceeded |
7 |
Incorrect |
327 ms |
856 KB |
Output isn't correct |
8 |
Incorrect |
226 ms |
604 KB |
Output isn't correct |
9 |
Incorrect |
54 ms |
772 KB |
Output isn't correct |
10 |
Incorrect |
163 ms |
600 KB |
Output isn't correct |
11 |
Incorrect |
182 ms |
600 KB |
Output isn't correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Execution timed out |
2025 ms |
9560 KB |
Time limit exceeded |
14 |
Execution timed out |
2051 ms |
5720 KB |
Time limit exceeded |
15 |
Incorrect |
1151 ms |
6288 KB |
Output isn't correct |
16 |
Execution timed out |
2041 ms |
4184 KB |
Time limit exceeded |
17 |
Execution timed out |
2015 ms |
24144 KB |
Time limit exceeded |
18 |
Execution timed out |
2072 ms |
23712 KB |
Time limit exceeded |
19 |
Execution timed out |
2057 ms |
22356 KB |
Time limit exceeded |
20 |
Execution timed out |
2066 ms |
20564 KB |
Time limit exceeded |
21 |
Execution timed out |
2041 ms |
55104 KB |
Time limit exceeded |
22 |
Correct |
240 ms |
53076 KB |
Output is correct |
23 |
Execution timed out |
2070 ms |
45748 KB |
Time limit exceeded |
24 |
Execution timed out |
2064 ms |
53600 KB |
Time limit exceeded |
25 |
Execution timed out |
2036 ms |
94288 KB |
Time limit exceeded |
26 |
Correct |
327 ms |
72272 KB |
Output is correct |
27 |
Correct |
1458 ms |
94180 KB |
Output is correct |
28 |
Execution timed out |
2013 ms |
94288 KB |
Time limit exceeded |
29 |
Execution timed out |
2061 ms |
94400 KB |
Time limit exceeded |
30 |
Correct |
1760 ms |
92244 KB |
Output is correct |
31 |
Execution timed out |
2029 ms |
60496 KB |
Time limit exceeded |
32 |
Execution timed out |
2041 ms |
94288 KB |
Time limit exceeded |