보물창고 블로그

백준 알고리즘 19237번 어른 상어 풀이 With Python 본문

알고리즘 풀이/백준 알고리즘

백준 알고리즘 19237번 어른 상어 풀이 With Python

홋 메 2020. 6. 30. 13:12
728x90

문제 링크: https://www.acmicpc.net/problem/19237

 

19237번: 어른 상어

첫 줄에는 N, M, k가 주어진다. (2 ≤ N ≤ 20, 2 ≤ M ≤ N2, 1 ≤ k ≤ 1,000) 그 다음 줄부터 N개의 줄에 걸쳐 격자의 모습이 주어진다. 0은 빈칸이고, 0이 아닌 수 x는 x번 상어가 들어있는 칸을 의미

www.acmicpc.net

청소년 상어 문제에서 성장한 어른 상어 문제입니다. 문제 설명 그대로 구현하는 시뮬레이션 문제입니다. while 문을 사용하여 매시간마다 time을 1씩 증가시켰고, 1000을 넘는 순간 -1을 리턴하고, 죽은 상어의 x좌표를 -1로 하여 체크를 건너뛰었습니다. 그리고 상어를 1마리씩 줄일 때마다 카운트 변수를 줄여 상어 1마리만 남았을 때의 시간을 반환하도록 solution함수를 만들었습니다. 소스코드는 아래와 같습니다. 

import sys

dx = [-1, 1, 0, 0]
dy = [0, 0, -1, 1]
n, m, k = map(int, sys.stdin.readline().split())
sharks = [[0, 0] for _ in range(m)]
board = []
smell = {}
for i in range(n):
    list1 = list(map(int, sys.stdin.readline().split()))
    board.append(list1)
    for j in range(n):
        if list1[j]:
            sharks[list1[j] - 1] = [i, j]
            smell[(i, j)] = [0, list1[j] - 1]
direction = list(map(int, sys.stdin.readline().split()))
dirlist = [list(map(int, sys.stdin.readline().split())) for _ in range(4 * m)]



def solution(board, smell, sharks, direction, dirlist):
    time = 0
    count = m
    while 1:
        time += 1
        if time > 1000:
            return -1
        for i in range(m):
            # 죽은거 처리
            if sharks[i][0] == -1:
                continue
            else:
                x, y = sharks[i]
                flag = 1
                # 상어 지금 바라보는 방향
                d = direction[i]
                dlist = dirlist[4 * i + d - 1]
                for d1 in dlist:
                    nx = x + dx[d1 - 1]
                    ny = y + dy[d1 - 1]
                    if -1 < nx < n and -1 < ny < n:
                        if smell.get((nx, ny)) is None or time - smell[(nx, ny)][0] > k:
                            flag = 0
                            # 가려는 칸에 다른 상어 있을 때
                            if board[nx][ny]:
                                count -= 1
                                if count == 1:
                                    return time
                                # 현재 상어가 강한 경우
                                if board[nx][ny] > i + 1:
                                    sharks[board[nx][ny] - 1][0] = -1
                                    board[nx][ny] = i + 1
                                    direction[i] = d1
                                    sharks[i][0] = nx
                                    sharks[i][1] = ny
                                # 현재 상어가 약한 경우
                                else:
                                    sharks[i][0] = -1
                            # 다른 상어 없을 때
                            else:
                                sharks[i][0] = nx
                                sharks[i][1] = ny
                                board[nx][ny] = i + 1
                                direction[i] = d1
                            board[x][y] = 0
                            break
                # 4방향에 다 냄새남을 때
                if flag:
                    for d1 in dlist:
                        nx = x + dx[d1 - 1]
                        ny = y + dy[d1 - 1]
                        if -1 < nx < n and -1 < ny < n and smell[(nx, ny)][1] == i:
                            direction[i] = d1
                            board[nx][ny] = i + 1
                            board[x][y] = 0
                            sharks[i][0]=nx
                            sharks[i][1]=ny
                            break
        for w in range(m):
            if sharks[w][0] == -1:
                continue
            else:
                smell[(sharks[w][0], sharks[w][1])] = [time, w]


ans = solution(board, smell, sharks, direction, dirlist)
print(ans)
Comments