이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#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 n, m;
vs s;
inline bool in(vi& v) {
return -1 < v[0] && v[0] < n && -1 < v[1] && v[1] < m;
}
inline char f(vi& v) {
return s[v[0]][v[1]];
}
inline int g(vi& v) {
return v[0]*m+v[1];
}
inline bool bad(vi& v) {
return f(v) == '.';
}
inline bool same(vi& v, vi& w) {
return f(v) == f(w);
}
int main() {
fast_io;
vvi adj = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
cin >> n >> m;
s = vs(n); for(auto &t: s) cin >> t;
queue<vi> q, nq; vi vis(n*m); int ret = 0;
vi src = {0, 0};
q.push(src), vis[g(src)] = 1;
while(!q.empty()) {
ret++; vvi add;
while(!q.empty()) {
vi v = q.front(); q.pop();
for(auto c: adj) {
vi w = {v[0]+c[0], v[1]+c[1]};
if(!in(w) || vis[g(w)] || bad(w)) continue;
if(same(v, w)) q.push(w); else
nq.push(w);
vis[g(w)] = 1;
}
}
while(!nq.empty()) {
vi v = nq.front(); nq.pop(), q.push(v);
}
}
cout << ret << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |