1210. [S/W 문제해결 기본] 2일차 - Ladder1
문제 풀이
- 1인 좌표부터 시작해서 2에 도달하면 멈추는 코드를 구현하였다.
- 예시인 10x10 배열에서는 정답이 잘 도출되었는데 input 파일의 예시는 통과하지 못했다.
import sys
from collections import deque
sys.stdin = open("input.txt", "r")
for i in range(1, 11):
n = int(input())
arr = [list(map(int, input().split())) for _ in range(100)]
def in_range(a, b):
if 0 <= a < 100 and 0 <= b < 100:
return True
return False
def bfs(a, b):
while in_range(a, b) and arr[a][b]:
a += 1
if in_range(a, b) and arr[a][b] == 2:
return True
elif in_range(a, b+1) and arr[a][b + 1]:
while in_range(a, b+1) and arr[a][b + 1]:
b += 1
a += 1
elif in_range(a, b-1) and arr[a][b - 1]:
while in_range(a, b-1) and arr[a][b - 1]:
b -= 1
a += 1
return False
xlist = []
for j in range(100):
if arr[0][j] == 1:
xlist.append([j, 0])
print(xlist)
for x, y in xlist:
if bfs(y, x):
print(x)
다른 정답 풀이
for _ in range(1, 11):
tc = int(input())
ladders = list(list(map(int, input().split())) for _ in range(100))
s = ladders[99].index(2)
x, y = 99, s
visited = [[0] * 100 for _ in range(100)]
while x != 0:
visited[x][y] = 1
if y - 1 >= 0 and ladders[x][y - 1] and visited[x][y - 1] == 0:
y -= 1
continue
elif y + 1 < 100 and ladders[x][y + 1] and visited[x][y + 1] == 0:
y += 1
continue
else:
x -= 1
answer = y
print(f'#{tc} {answer}')
참고 블로그