μ΄ λ¬Έμ λ νλ¨Όμ κ΅νμ΄ μμ΄μ νΉλ³ν TILμ λ¨κ²¨λ΄.
#include <iostream>
#include <vector>
#include <limits>
using namespace std;
int MAX_INT = numeric_limits<int>::max();
int camStatus[8] = {0};
vector<int> camType;
vector<pair<int, int>> camLoc;
vector<int> choices = {-1, 4, 2, 4, 4, 1};
int N, M, C;
vector<vector<int>> map;
int min_blind = MAX_INT;
vector<vector<int>> dirs = {
{0, 1}, // μ€λ₯Έμͺ½
{-1, 0}, // μμͺ½
{0, -1}, // μΌμͺ½
{1, 0}}; // μλ«μͺ½
void mark(vector<vector<int>> &mm, int n, int m, int dir)
{
// n,mμ κΈ°μ μΌλ‘ λ°©ν₯μ λ°λΌ μ κ°μκ°λ₯ν ꡬκ°μ νμν¨
int cn = n + dirs[dir][0];
int cm = m + dirs[dir][1];
while (0 <= cn && cn < N && 0 <= cm && cm < M)
{
if (mm[cn][cm] == 6)
{
//λ²½μ λ§λ¬μΌλ λ©μΆ€
break;
}
if (mm[cn][cm] == 0)
{
// λΉ κ³΅κ°μ΄λκΉ κ°μκ° κ°λ₯νλ€κ³ μ¨μ€
// λ§μ½ μΉ΄λ©λΌλ©΄ μ건λλ¦¬κ³ κ·Έλ₯ λμ΄κ°
mm[cn][cm] = 7; //7μ κ°μκ° λλ€λ λ»
}
cn += dirs[dir][0];
cm += dirs[dir][1];
}
return;
}
int calc(int until) // untilλ²μ§Έ μΉ΄λ©λΌκΉμ§ λ°μν΄μ μ¬κ°μ§λμ κ°―μλ₯Ό κ³μ°
{
vector<vector<int>> tmap = map; // copy
for (int i = 0; i <= until; i++)
{
int ctype = camType[i];
int cStatus = camStatus[i];
int n = camLoc[i].first;
int m = camLoc[i].second;
if (ctype == 1)
{
mark(tmap, n, m, cStatus);
}
else if (ctype == 2)
{
if (cStatus == 0)
{
mark(tmap, n, m, 0);
mark(tmap, n, m, 2);
}
else if (cStatus == 1)
{
mark(tmap, n, m, 1);
mark(tmap, n, m, 3);
}
}
else if (ctype == 3)
{
int dd = (cStatus + 1) % 4;
mark(tmap, n, m, cStatus);
mark(tmap, n, m, dd);
}
else if (ctype == 4)
{
for (int d = 0; d < 4; d++)
{
if (d == cStatus)
{
continue;
}
mark(tmap, n, m, d);
}
}
else if (ctype == 5)
{
for (int d = 0; d < 4; d++)
{
mark(tmap, n, m, d);
}
}
}
int bcnt = 0;
for (int n = 0; n < N; n++)
{
for (int m = 0; m < M; m++)
{
if (tmap[n][m] == 0)
{
bcnt++;
}
}
}
return bcnt;
}
void dfs(int depth)
{
if (depth == camLoc.size())
{
int cost = calc(depth - 1);
if (cost < min_blind)
{
min_blind = cost;
}
return;
}
int type = camType[depth];
for (int c = 0; c < choices[type]; c++)
{
// μΉ΄λ©λΌμ μ’
λ₯μ λ°λΌ κ°λ₯ν κ²½μ°μ μλ₯Ό νλ 골λΌμ μ μ₯νκ³ λ€μ λ¨κ³λ‘ λμ΄κ°
camStatus[depth] = c;
dfs(depth + 1);
}
}
int main()
{
cin >> N >> M; // height, width
map = vector<vector<int>>(N, vector<int>(M));
for (int n = 0; n < N; n++)
{
for (int m = 0; m < M; m++)
{
int t;
cin >> t;
map[n][m] = t;
if (0 < t && t < 6)
{
camLoc.push_back(make_pair(n, m));
camType.push_back(t);
}
}
}
dfs(0);
cout << min_blind;
return 0;
}