반응형
https://www.acmicpc.net/problem/15684
15684번: 사다리 조작
사다리 게임은 N개의 세로선과 M개의 가로선으로 이루어져 있다. 인접한 세로선 사이에는 가로선을 놓을 수 있는데, 각각의 세로선마다 가로선을 놓을 수 있는 위치의 개수는 H이고, 모든 세로선
www.acmicpc.net



This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 백준 17144. 미세먼지 안녕! (0) | 2020.05.20 |
---|---|
[Python] 프로그래머스. 2019 카카오 recruit - 블록 게임 (Level 4) (0) | 2020.05.19 |
[Python] 백준 11003. 최솟값 찾기 (0) | 2020.05.16 |
[Python] 프로그래머스. 소수 만들기 (Level 2) (0) | 2020.05.11 |
[Python] 프로그래머스. 점프와 순간이동 (Level 2) (0) | 2020.05.08 |