This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
int n, m;
vector<vector<char>>grid;
vector<vector<bool>>vis;
bool found = 0;
bool check(int i, int j){
return (i >= 0 && i < n && j >= 0 && j < m);
}
void rec(int i, int j, char cur){
vis[i][j] = 1;
if(grid[i][j] == cur){
found = 1;
grid[i][j] = 'x';
}
if(check(i + 1, j) && !vis[i + 1][j] && (grid[i + 1][j] == 'x' || grid[i + 1][j] == cur))rec(i + 1, j, cur);
if(check(i, j + 1) && !vis[i][j + 1] && (grid[i][j + 1] == 'x' || grid[i][j + 1] == cur))rec(i, j + 1, cur);
if(check(i - 1, j) && !vis[i - 1][j] && (grid[i - 1][j] == 'x' || grid[i - 1][j] == cur))rec(i - 1, j, cur);
if(check(i, j - 1) && !vis[i][j - 1] && (grid[i][j - 1] == 'x' || grid[i][j - 1] == cur))rec(i, j - 1, cur);
}
int main(){
// ios_base::sync_with_stdio(0);
// cin.tie(0);
cin >> n >> m;
grid.assign(n, vector<char>(m));
vis.assign(n, vector<bool>(m));
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
cin >> grid[i][j];
}
}
char c = grid[0][0];
int cnt = 0;
rec(0, 0, c);
while(found){
found = 0;
cnt++;
if(c == 'T')c = 'B';
else c = 'T';
for(int i = 0;i < n;++i){
for(int j = 0;j < m;++j){
vis[i][j] = 0;
}
}
rec(0, 0, c);
}
cout << cnt;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |