https://www.acmicpc.net/problem/15684
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
int n, m, h;
int v[12][32];
int answer = -1;
bool Move(int cur, int index) {
int start = cur;
for (int i = index; i <= h; i++) {
if (v[cur][i] == 1) {
cur++;
}
else if (v[cur - 1][i] == 1) {
cur--;
}
}
if (start == cur) return true;
else return false;
}
bool CheckRight() {
bool flag = true;
for (int i = 1; i <= n; i++) {
flag = Move(i, 1);
if (flag == false) break;
}
return flag;
}
void MakeBridge(int loc, int sz, int count) {
if (count == sz) {
if (CheckRight() == true) {
answer = sz;
}
return;
}
for (int i = loc; i < n; i++) {
for (int j = 1; j <= h; j++) {
if (v[i][j] == -1 && v[i + 1][j] == -1 && v[i - 1][j] == -1) {
v[i][j] = 1;
MakeBridge(i, sz, count + 1);
v[i][j] = -1;
}
}
}
}
int main(void) {
cin >> n >> m >> h;
memset(v, -1, sizeof(v));
for (int i = 0; i < m; i++) {
int x, y;
cin >> x >> y;//위치, 세로선
v[y][x] = 1;
}
if (CheckRight() == true) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= 3; i++) {
MakeBridge(1, i, 0);
if (answer != -1) {
break;
}
}
cout << answer << endl;
}
'백준 알고리즘 > 브루트포스' 카테고리의 다른 글
백준 2048 (Easy) C++ (0) | 2019.10.19 |
---|---|
백준 15686번 C++ (0) | 2019.04.13 |
백준 14502번 C++ (0) | 2019.04.09 |
백준 1018번 C++ (0) | 2019.02.04 |
백준 14889번 C++ (0) | 2019.01.28 |