Home BOJ. Cheese (2638)
Post
Cancel

BOJ. Cheese (2638)

[Link] https://www.acmicpc.net/problem/2638


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <cstring>
// #include <bits/stdc++.h>
using namespace std;
#define for0(i, n) for(int i = 0; i < n; i++)
#define for1(i, n) for(int i = 1; i < n; i++)
#define fori(s, e) for(int i = s; i < e; i++)
#define ll long long
#define endl "\n"
#define nulls '\0'
#define dkdk " "
const int MAX = 5e5 + 1;
int n, m, time;
bool mm[100][100];
bool visit[100][100];
int deg[100][100];
int di[4] = { -1, 0, 1, 0 };
int dj[4] = { 0, 1, 0, -1 };
queue<pair<int,int>> q;

void bfs(int i, int j) {
  // q = {}; // clear;
  if(visit[i][j]) return;
  q.push({ i, j });
  while(!q.empty()) {
    pair<int, int> p = q.front(); q.pop();
    int ci = p.first, cj = p.second;
    if(visit[ci][cj]) continue;
    visit[ci][cj] = true;
    for0(k, 4) {
      int ai = ci + di[k], aj = cj + dj[k];
      if(ai < 0 || aj < 0 || ai>= n || aj >= m) continue;
      if(visit[ai][aj]) continue;
      if(mm[ai][aj]) { deg[ai][aj]++; continue; }
      q.push({ ai, aj });
    }
  }
}

void solve() {
  while(true) {
    bool cheese = false;
    for0(i, n) for0(j, m) if(mm[i][j]) { cheese = true; break; }
    if(cheese == false) break;
    for0(i, n) for0(j, m) { visit[i][j] = false; deg[i][j] = 0; }

    for0(i, n) { bfs(i, 0); bfs(i, m-1); }
    for0(j, m) { bfs(0, j); bfs(n-1, j); }

    for0(i, n) for0(j, m) if(deg[i][j] >= 2) mm[i][j] = false;
    time++;
  }

}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for0(i, n) for0(j, m) cin >> mm[i][j];
  solve();
  cout << time;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.