This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
char matrix[105][105];
bool vis[105][105];
int dx[4] = {0, 1, 0, -1};
int dy[4] = {-1, 0, 1, 0};
#define ONLINE_JUDGE
void solve() {
int r, c;
cin >> r >> c;
int cnt = 0;
for(int i = 1; i <= r; i++) {
for(int j = 1; j <= c; j++) {
cin >> matrix[i][j];
cnt += (matrix[i][j] != '*');
}
}
auto in = [&](int x, int y) {
return 1 <= x && x <= r && 1 <= y && y <= c;
};
auto ok = [&]() -> int {
memset(vis, false, sizeof(vis));
vector <pair <int, int>> vec;
queue <pair <int, int>> q;
q.emplace(1, 1);
while(!q.empty()) {
auto [x, y] = q.front();
q.pop();
if(vis[x][y]) {
continue;
}
vis[x][y] = true;
vec.emplace_back(x, y);
for(int i = 0; i < 4; i++) {
int _x = x + dx[i], _y = y + dy[i];
if(in(_x, _y) && vis[_x][_y] == false && matrix[_x][_y] == matrix[x][y]) {
q.emplace(_x, _y);
}
}
}
char c = (matrix[1][1] == 'B' ? 'T' : 'B');
for(auto [a, b] : vec) {
matrix[a][b] = c;
}
return int(vec.size());
};
int ans = 1;
while(ok() < cnt) {
ans++;
}
cout << ans << "\n";
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |