보물창고 블로그

SW Expert Academy 1953. [모의 SW 역량테스트] 탈주범 검거 본문

알고리즘 풀이/SW Expert Academy

SW Expert Academy 1953. [모의 SW 역량테스트] 탈주범 검거

홋 메 2020. 3. 4. 17:53
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))
Comments