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, cnt, loop;
vector<string> h, v;
vector<vector<bool>> visited;
struct Pos {
Pos(int xi, int yi):x(xi),y(yi){}
int x, y;
};
Pos temp(0, 0);
queue<Pos> q;
vector<Pos> c;
bool srt(Pos a, Pos b) {
return a.x < b.x;
}
int main() {
cin >> n >> m;
h = vector<string>(n+1), v = vector<string>(n), visited = vector<vector<bool>>(n, vector<bool>(m, false));
for(int i = 0; i <= n; i++) cin >> h[i];
for(int i = 0; i < n; i++) cin >> v[i];
for(int i = 0; i < n; i++) {
for(int j = 0; j < m; j++) {
if(!visited[i][j] && (h[i][j] == '0' || h[i+1][j] == '0' || v[i][j] == '0' || v[i][j+1] == '0')) {
cnt = 0, loop = 1;
q.push({i, j});
while(q.size()) {
temp = q.front();
q.pop();
if(visited[temp.x][temp.y]) continue;
cnt++;
visited[temp.x][temp.y] = true;
if((temp.x == 0 && h[temp.x][temp.y] == '0') || (temp.x == n-1 && h[temp.x+1][temp.y] == '0') || (temp.y == 0 && v[temp.x][temp.y] == '0') || (temp.y == m-1 && v[temp.x][temp.y+1] == '0')) loop = 0;
if(temp.x > 0 && h[temp.x][temp.y] == '0' && !visited[temp.x-1][temp.y]) q.push(Pos(temp.x-1, temp.y));
if(temp.x < n-1 && h[temp.x+1][temp.y] == '0' && !visited[temp.x+1][temp.y]) q.push(Pos(temp.x+1, temp.y));
if(temp.y > 0 && v[temp.x][temp.y] == '0' && !visited[temp.x][temp.y-1]) q.push(Pos(temp.x, temp.y-1));
if(temp.y < m-1 && v[temp.x][temp.y+1] == '0' && !visited[temp.x][temp.y+1]) q.push(Pos(temp.x, temp.y+1));
}
c.push_back(Pos(cnt, loop));
}
}
}
sort(c.begin(), c.end(), srt);
//for(auto i : c) cout << i.x << " " << i.y << "\n";
cout << max(min((c[0].y ? 8 : 4)-c[0].x-c[1].x, c[1].x-c[0].x), min((c[1].y ? 8 : 4)-c[0].x-c[1].x, c[0].x-c[1].x)) << "\n";
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |