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
vi& operator+=(vi& lhs, const vi& rhs) {
int n = lhs.size();
for(int i = 0; i < n; i++) lhs[i] += rhs[i];
return lhs;
}
vi operator+(vi lhs, const vi& rhs) {
lhs += rhs;
return lhs;
}
int main() {
fast_io;
vvi adj = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
int n, m; cin >> n >> m;
vs s(n); for(auto &t: s) cin >> t;
auto in = [&] (vi v) {
return -1 < v[0] && v[0] < n && -1 < v[1] && v[1] < m;
};
auto f = [&] (vi v) {
return s[v[0]][v[1]];
};
auto g = [&] (vi v) {
return v[0]*m+v[1];
};
auto bad = [&] (vi v) {
return f(v) == '.';
};
auto same = [&] (vi v, vi w) {
return f(v) == f(w);
};
queue<vi> q, nq; vi vis(n*m); int ret = 0;
q.push({0, 0}), vis[g({0, 0})] = 1;
while(!q.empty()) {
ret++;
while(!q.empty()) {
vi v = q.front(); q.pop(), nq.push(v);
for(auto c: adj) {
vi w = v+c;
if(!in(w) || vis[g(w)] || bad(w) || !same(v, w)) continue;
q.push(w), vis[g(w)] = 1;
}
}
while(!nq.empty()) {
vi v = nq.front(); nq.pop();
for(auto c: adj) {
vi w = v+c;
if(!in(w) || vis[g(w)] || bad(w)) continue;
q.push(w), vis[g(w)] = 1;
}
}
}
cout << ret << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |