ํ์ผ์...
๐๏ธ ๊ฒช์ ๋ฌธ์
- ๋ฐฑํธ๋ํน์ผ๋ก ํธ๋ ๋ฐฉ๋ฒ์ ์์ ๋ชฐ๋ผ์ 1~3์ค๊น์ง for๋ฌธ์ผ๋ก ๊ตฌํํด ํ๋ ค๊ณ ํ์์ผ๋, ์ฝ๋๊ฐ ๊ธธ์ด์ง๋ค๋ณด๋ ๋ก์ง์ ์ค๋ฅ๊ฐ ์๊ฒผ์.
- ๊ทธ๋์ ๋ฌธ์ ๋ฅผ ํ์ง ๋ชปํ๋ค ใ
ใ
๋ณต์ต์ ํ ๋ ์ฐธ๊ณ ํ๋ ค๊ณ ๊ฐ์ฅ ๋ง์์ ๋๋ ํ์ด๋ฅผ ๋ณด๊ณ ๊ธ์ ์ ๋๋ค.
๐ ํด๊ฒฐ ๋ฐฉ๋ฒ
- DFS ๋ฐฑํธ๋ํน์ ์ ์ฉํจ.
- check() ํจ์๋ ์ด์ ๋ํด ์ํํ๋ฉฐ
- ํ์ฌ ์์น์ true๊ฐ ์์ผ๋ฉด ๋ค๋ฆฌ๋ฅผ ํ๊ณ ์ฐ์ธก์ผ๋ก ์ด๋ํ๋ฏ๋ก pos๋ฅผ ++,
- ํ์ฌ ์์น ์ข์ธก์ true๊ฐ ์์ผ๋ฉด ๋ค๋ฆฌ๋ฅผ ํ๊ณ ์ข์ธก์ผ๋ก ์ด๋ํ๋ฏ๋ก pos๋ฅผ --ํด์
- ์ต์ข
pos๊ฐ ์์ํ ์ด๊ณผ ๋์ผํ๋ฉด ์กฐ๊ฑด์ ๋ง์กฑํ๋ค๊ณ ํ๋จํจ.
๐ ์ฝ๋
#include <iostream>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
int N, M, H;
int minCnt = 4;
vector<vector<bool>> ladder(31, vector<bool>(11, false));
bool check() {
for (int i=1; i<=N; i++) {
int pos = i;
for (int j=1; j<=H; j++) {
if (ladder[j][pos]) pos++;
else if (ladder[j][pos-1]) pos--;
}
if (pos != i) return false;
}
return true;
}
void DFS(int h, int cnt) {
if (cnt >= minCnt) return;
if (check()) minCnt = cnt;
for (int i=h; i<=H; i++) {
for (int j=1; j<N; j++) {
if (ladder[i][j] || ladder[i][j-1] || ladder[i][j+1]) continue;
ladder[i][j] = true;
DFS(i, cnt+1);
ladder[i][j] = false;
}
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> N >> M >> H;
for (int i=0; i<M; i++) {
int a, b;
cin >> a >> b; // 1-base
ladder[a][b] = true;
}
DFS(1,0);
if (minCnt > 3) cout << -1;
else cout << minCnt;
return 0;
}