[코드트리] 싸움땅

ewillwin·2024년 4월 10일
0

import sys
input = sys.stdin.readline

dx = (-1, 0, 1, 0)
dy = (0, 1, 0, -1)

N, M, K = map(int, input().split())

board = [[[] for m in range(N)] for n in range(N)]
for n in range(N):
    tmp = list(map(int, input().split()))
    for m in range(N):
        if tmp[m] != 0: board[n][m].append(tmp[m])

player = dict(); player_pos = dict()
for m in range(M):
    x, y, d, s = map(int, input().split())
    player[m+1] = [x-1, y-1, d, s, 0] #[x, y, d, s, g]
    player_pos[(x-1, y-1)] = m+1

def changeGun(m, x, y): #player 번호, 현재 위치
    global player

    max_, idx_ = player[m][4], -1
    for i in range(len(board[x][y])):
        if board[x][y][i] > max_:
            max_ = board[x][y][i]
            idx_ = i
    if idx_ != -1:
        if player[m][4] != 0:
            board[x][y][idx_] = player[m][4]
        else:
            board[x][y].pop(idx_)
        player[m][4] = max_

def processBattle(win, lose, x, y):
    global player, player_pos

    score_stat[win] += abs((player[win][3]+player[win][4]) - (player[lose][3]+player[lose][4]))

    ### loser 총 내려놓기
    if player[lose][4] != 0:
        board[x][y].append(player[lose][4])
        player[lose][4] = 0
    
    ### winner, loser 위치 미리 삭제
    del player_pos[(player[win][0], player[win][1])]
    del player_pos[(player[lose][0], player[lose][1])]

    ### loser 한칸 이동
    d = player[lose][2]
    while True:
        nx, ny = x+dx[d], y+dy[d]
        if (nx, ny) not in player_pos and 0<=nx<N and 0<=ny<N:
            if board[nx][ny]:
                changeGun(lose, nx, ny)
            player[lose][0] = nx; player[lose][1] = ny
            player_pos[(nx, ny)] = lose
            player[lose][2] = d
            break
        d = (d+1)%4

    ### winner 위치 처리
    player[win][0] = x; player[win][1] = y
    player_pos[(x, y)] = win

    ### winner 총 바꾸기
    changeGun(win, x, y)

score_stat = [0] * (M+1)
for k in range(K):
    for m in range(1, M+1):
        x, y, d, s, g = player[m]
        nx, ny = x+dx[d], y+dy[d]
        if nx<0 or nx>=N or ny<0 or ny>=N: d = (d+2)%4; nx, ny = x+dx[d], y+dy[d]; player[m][2] = d

        if (nx, ny) in player_pos: ###이동한 칸에 다른 player가 있는 경우###
            n = player_pos[(nx, ny)]

            if (player[m][3]+player[m][4]) > (player[n][3]+player[n][4]):
                processBattle(m, n, nx, ny)
            elif (player[m][3]+player[m][4]) == (player[n][3]+player[n][4]):
                if player[m][3] > player[n][3]:
                    processBattle(m, n, nx, ny)
                else:
                    processBattle(n, m, nx, ny)
            else:
                processBattle(n, m, nx, ny)

        else:
            if board[nx][ny]: ###이동한 칸에 총이 있는 경우/ else: 이동한 칸에 아무것도 없는 경우###
                changeGun(m, nx, ny)
            player[m][0] = nx; player[m][1] = ny
            del player_pos[(x, y)]; player_pos[(nx, ny)] = m

print(" ".join(map(str, score_stat[1:])))
profile
💼 Software Engineer @ LG Electronics | 🎓 SungKyunKwan Univ. CSE

0개의 댓글