공부하고 기록하는, 경제학과 출신 개발자의 노트

프로그래밍/코딩테스트 문제풀이

[Python] 백준 15684. 사다리 조작

inspirit941 2020. 5. 18. 21:15
반응형

https://www.acmicpc.net/problem/15684

 

15684번: 사다리 조작

사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선

www.acmicpc.net

 

import sys
n, m, h = map(int, sys.stdin.readline().split())
table = [[0 for _ in range(n+2)] for _ in range(h)]
# table[y][x] = x와 x+1이 y번째 row에서 연결되어 있다.
answer = 4
for _ in range(m):
a, b = map(int, sys.stdin.readline().split())
table[a-1][b] = 1
def check(maps):
# col은 1부터 len(maps[0])-2개까지.
for col in range(1, len(maps[0])-2):
start = col
for row in range(len(maps)):
if maps[row][col] == 1:
col += 1
elif maps[row][col-1] == 1:
col -= 1
# 도착지점과 시작지점의 column이 다른 경우
if col != start:
return False
return True
def ladder_build_and_search(maps, cnt, limit):
global answer
if cnt == limit:
if check(maps):
answer = min(answer, limit)
return
for y in range(len(maps)):
for x in range(1, len(maps[0])-2):
if maps[y][x] != 1 and maps[y][x-1] != 1 and maps[y][x+1] != 1:
maps[y][x] = 1
ladder_build_and_search(maps, cnt+1, limit)
maps[y][x] = 0
for i in range(4):
ladder_build_and_search(table, 0, i)
if answer != 4:
break
if answer != 4:
print(answer)
else:
print(-1)
반응형