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 <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
#define fast_io ios::sync_with_stdio(0), cin.tie(0)
#define vi vector<int>
#define vvi vector<vi>
#define vs vector<string>
#define pb push_back
int main() {
fast_io;
int n, m; cin >> n >> m;
vs s(n); for(auto &t: s) cin >> t;
auto in = [&] (int x) {
return -1 < x && x < n*m;
};
auto bad = [&] (int x) {
return s[x/m][x%m] == '.';
};
auto same = [&] (int x, int y) {
int xr = x/m, xc = x%m, yr = y/m, yc = y%m;
return s[xr][xc] == s[yr][yc];
};
queue<int> q, nq; vi vis(n*m), adj = {-m, -1, 1, m}; int ret = 0;
q.push(0), vis[0] = 1;
while(!q.empty()) {
ret++;
while(!q.empty()) {
int x = q.front(); q.pop(), nq.push(x);
for(int c: adj) {
int y = x+c;
if(!in(y) || vis[y] || bad(y) || !same(x, y)) continue;
q.push(y), vis[y] = 1;
}
}
while(!nq.empty()) {
int x = nq.front(); nq.pop();
for(int c: adj) {
int y = x+c;
if(!in(y) || vis[y] || bad(y)) continue;
q.push(y), vis[y] = 1;
}
}
}
cout << ret << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |