일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 1038번
- 미세먼지 안녕!
- 감소하는 수
- 프로그래머스
- 15686번
- 베스트엘범
- SW Expert Academy
- 거울 설치
- QueryDSL 기초
- python
- 빛의 경로 사이클
- 9095번
- 경주로 건설
- 어른 상어
- 17144번
- 수식 최대화
- 14499번
- 2020 카카오 인턴십
- 키패드 누르기
- 1789번
- 백준 알고리즘
- 스타트 택시
- HTML 기초
- SW ExpertAcademy
- 19238번
- 보석 쇼핑
- 16234번
- 파이썬
- 12865번
- 12869번
Archives
- Today
- Total
보물창고 블로그
SW Expert Academy 1953. [모의 SW 역량테스트] 탈주범 검거 본문
728x90
문제 링크: https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PpLlKAQ4DFAUq
SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
이 문제의 핵심은 각 파이프가 주어졌을 때 갈 수 있는지 없는지를 판별하는 것이 핵심이다.
해결한 코드는 아래와 같다.
from collections import deque
def solution(map1, n, m, r, c, l, visit, test):
dx = [-1, 0, 1, 0]
dy = [0, -1, 0, 1]
count = 0
queue = deque([[r, c, 1]])
while queue:
x, y, t = queue.popleft()
if t > l:
continue
else:
if visit[x][y] != test:
count += 1
visit[x][y] = test
if map1[x][y] == 1:
for d in range(4):
nx = x + dx[d]
ny = y + dy[d]
if -1 < nx < n and -1 < ny < m:
c1 = map1[nx][ny]
if d == 0 and (c1 == 1 or c1 == 2 or c1 == 5 or c1 == 6) and visit[nx][ny] != test:
queue.append([nx, ny, t + 1])
if d == 1 and (c1 == 1 or c1 == 3 or c1 == 4 or c1 == 5) and visit[nx][ny] != test:
queue.append([nx, ny, t + 1])
if d == 2 and (c1 == 1 or c1 == 2 or c1 == 4 or c1 == 7) and visit[nx][ny] != test:
queue.append([nx, ny, t + 1])
if d == 3 and (c1 == 1 or c1 == 3 or c1 == 6 or c1 == 7) and visit[nx][ny] != test:
queue.append([nx, ny, t + 1])
elif map1[x][y] == 2:
for d in range(2):
nx = x + dx[2 * d]
if -1 < nx < n:
c1 = map1[nx][y]
if d == 0 and (c1 == 1 or c1 == 2 or c1 == 5 or c1 == 6) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
if d == 1 and (c1 == 1 or c1 == 2 or c1 == 4 or c1 == 7) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
elif map1[x][y] == 3:
for d in range(2):
ny = y + dy[1 + 2 * d]
if -1 < ny < m:
c1 = map1[x][ny]
if d == 0 and (c1 == 1 or c1 == 3 or c1 == 4 or c1 == 5) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
if d == 1 and (c1 == 1 or c1 == 3 or c1 == 6 or c1 == 7) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
elif map1[x][y] == 4:
nx = x - 1
if -1 < nx:
c1 = map1[nx][y]
if (c1 == 1 or c1 == 2 or c1 == 5 or c1 == 6) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
ny = y + 1
if ny < m:
c1 = map1[x][ny]
if (c1 == 1 or c1 == 3 or c1 == 6 or c1 == 7) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
elif map1[x][y] == 5:
nx = x + 1
if nx < n:
c1 = map1[nx][y]
if (c1 == 1 or c1 == 2 or c1 == 4 or c1 == 7) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
ny = y + 1
if ny < m:
c1 = map1[x][ny]
if (c1 == 1 or c1 == 3 or c1 == 6 or c1 == 7) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
elif map1[x][y] == 6:
nx = x + 1
if nx < n:
c1 = map1[nx][y]
if (c1 == 1 or c1 == 2 or c1 == 4 or c1 == 7) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
ny = y - 1
if -1 < ny:
c1 = map1[x][ny]
if (c1 == 1 or c1 == 3 or c1 == 4 or c1 == 5) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
elif map1[x][y] == 7:
nx = x - 1
if -1 < nx:
c1 = map1[nx][y]
if (c1 == 1 or c1 == 2 or c1 == 5 or c1 == 6) and visit[nx][y] != test:
queue.append([nx, y, t + 1])
ny = y - 1
if -1 < ny:
c1 = map1[x][ny]
if (c1 == 1 or c1 == 3 or c1 == 4 or c1 == 5) and visit[x][ny] != test:
queue.append([x, ny, t + 1])
return count
t = int(input())
visit = [[0] * 50 for _ in range(50)]
for test in range(1, t + 1):
n, m, r, c, l = map(int, input().split())
map1 = [list(map(int, input().split())) for _ in range(n)]
answer = solution(map1, n, m, r, c, l, visit, test)
print('#{} {}'.format(test, answer))
'알고리즘 풀이 > SW Expert Academy' 카테고리의 다른 글
SW Expert Academy 2115. [모의 SW 역량테스트] 벌꿀채취 (0) | 2020.03.04 |
---|---|
SW Expert Academy 2112. [모의 SW 역량테스트] 보호 필름 (0) | 2020.03.04 |
SW Expert Academy 1952. [모의 SW 역량테스트] 수영장 (0) | 2020.03.04 |
SW Expert Academy 2382. [모의 SW 역량테스트] 미생물 격리 (0) | 2020.03.04 |
SW Expert Academy 2383. [모의 SW 역량테스트] 점심 식사시간 (0) | 2020.03.04 |
Comments