백준
1. DFS
import sys
input = sys.stdin.readline
n, m, h = map(int, input().split())
ladder = [[0] * n for _ in range(h)]
answer = sys.maxsize
for _ in range(m):
a, b = map(int, input().split())
ladder[a - 1][b - 1] = 1
def check():
for i in range(n):
k = i
for j in range(h):
if ladder[j][k]:
k += 1
elif k > 0 and ladder[j][k-1]:
k -= 1
if i != k:
return False
return True
def dfs(count, x, y):
global answer
if answer <= count:
return
if check():
answer = min(answer, count)
if count == 3:
return
for i in range(x, h):
if i == x:
k = y
else:
k = 0
for j in range(k, n - 1):
if ladder[i][j]:
j += 1
else:
ladder[i][j] = 1
dfs(count + 1, i, j + 2)
ladder[i][j] = 0
dfs(0,0,0)
if answer < 4:
print(answer)
else:
print(-1)