Home BOJ. Inspect (15683)
Post
Cancel

BOJ. Inspect (15683)

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


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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <algorithm>
#include <iostream>
#include <vector>
// #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++)
typedef long long ll;
const int INF = 987654321;

int n, m, ans = INF;
int mm[8][8];
int di[4] = { -1, 0, 1, 0 };
int dj[4] = { 0, 1, 0, -1 };
bool inspect[8][8];
vector<int> cctv_vec;
vector<int> rot_vec;
// Long initialization code because of the auto-build parse error
vector<int> cctv_1 = {1};
vector<int> cctv_2 = {1, 3};
vector<int> cctv_3 = {0, 1};
vector<int> cctv_4 = {0, 1, 3};
vector<int> cctv_5 = {0, 1, 2, 3};
cctvs.push_back(cctv_1);
cctvs.push_back(cctv_2);
cctvs.push_back(cctv_3);
cctvs.push_back(cctv_4);
cctvs.push_back(cctv_5);

void inspect_dir(int ci, int cj, int dir) {
  while(true) {
    inspect[ci][cj] = true;
    int ai = ci + di[dir], aj = cj + dj[dir];
    if(ai < 0 || aj < 0 || ai >= n || aj >= m) break;
    if(mm[ai][aj] == 6) break;
    ci = ai, cj = aj;
  }
}

void clear_inspect() {
  for0(i, 8) for0(j, 8) inspect[i][j] = false;
}

void dfs(int nth) {
  if(nth == cctv_vec.size()) {
    clear_inspect();
    int cctv_order = 0;
    for0(i, n) {
      for0(j, m) {
        if(mm[i][j] == 0) continue;
        else if(mm[i][j] == 6) {
          inspect[i][j] = true;
          continue;
        }
        int cctv = cctv_vec[cctv_order];
        for(int ldir: cctvs[cctv - 1]) {
          ldir += rot_vec[cctv_order];
          if(ldir >= 4) ldir -= 4;
          inspect_dir(i, j, ldir);
        }
        cctv_order++;
      }
    }
    int non_inspect_cnt = 0;
    for0(i, n) for0(j, m) if(inspect[i][j] == false) non_inspect_cnt++;
    ans = min(ans, non_inspect_cnt);
    return;
  }
  int examin_cnt;
  switch(cctv_vec[nth]) {
    case 5: examin_cnt = 1; break;
    case 2: examin_cnt = 2; break;
    default: examin_cnt = 4; break;
  };
  for(int i = 0; i < examin_cnt; i++) {
    rot_vec[nth]+=i;
    dfs(nth + 1);
    rot_vec[nth]-=i;
  }
}

void solve() {
  rot_vec.resize(cctv_vec.size(), 0);
  dfs(0);
}

int main() {
  ios_base::sync_with_stdio(0);cin.tie(0); cout.tie(0);
  cin >> n >> m;
  int cctv_cnt = 0;
  for0(i, n)
    for0(j, m) {
      cin >> mm[i][j];
      if(mm[i][j] != 0 && mm[i][j] != 6) cctv_vec.push_back(mm[i][j]);
    }

  solve();
  cout << ans;
  return 0;
}
This post is licensed under CC BY 4.0 by the author.