Home BOJ. Cheese (2636)
Post
Cancel

BOJ. Cheese (2636)

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


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
69
70
71
72
73
74
#include <algorithm>
#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <queue>
// #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 endl "\n"
typedef long long ll;
const int INF = 987654321;
const int MAX = 100;

int n, m;
bool air[100][100];
bool cheese[100][100];
bool visit[100][100];
bool update[100][100];
int di[4] = { -1, 0, 1, 0 };
int dj[4] = { 0, 1, 0, -1 };

void solve() {
  queue<pair<int, int>> q;
  int left_cheese = 0, time = 0;
  for0(i, n) for0(j, m) if(cheese[i][j]) left_cheese++;
  if(!left_cheese) {
    cout << 0 << endl << 0;
    return;
  }
  while(true) {
    time++;
    for0(i, n) for0(j, m) visit[i][j] = update[i][j] = false;
    q.push(make_pair(0, 0));
    while(!q.empty()) {
      pair<int, int> p = q.front(); q.pop();
      if(visit[p.first][p.second]) continue;
      visit[p.first][p.second] = true;
      for0(i, 4) {
        int ai = p.first + di[i], aj = p.second + dj[i];
        if(ai < 0 || aj < 0 || ai >= n || aj >= m) continue;
        if(visit[ai][aj]) continue;
        if(cheese[ai][aj]) {
          update[ai][aj] = true;
          visit[ai][aj] = true;
          continue;
        }
        q.push(make_pair(ai, aj));
      }
    }
    int cheese_cnt = 0;
    for0(i, n) for0(j, m) {
      if(update[i][j]) { cheese[i][j] = false; }
      else if(cheese[i][j]) cheese_cnt++;
    }
    if(cheese_cnt) left_cheese = cheese_cnt;
    else break;
  }
  cout << time << endl << left_cheese;
}

int main() {
  ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  cin >> n >> m;
  for0(i, n) for0(j, m) {
    int in; cin >> in;
    if(!in) continue;
    cheese[i][j] = true;
  }

  solve();
}
This post is licensed under CC BY 4.0 by the author.