즐거운 PS 👩‍💻🥰

[백준-파이썬] 20165: 인내의 도미노 장인 호석

dalin❤️ 2022. 2. 22. 23:08

문제 풀러 가기!

꼼꼼히 천천히 구현했다~
사 방향을 일일이 코드로 짜서 좀 긴데ㅠ, 줄일 수 있을 것 같기도 하다.

🧐 헷갈렸던 부분

  1. 이미 넘어진 격자의 도미노를 공격수가 무너뜨리려고 할 때는 공격수 점수를 카운트 하면 안된다!
  2. x, y, d 는 map(int,input().split())으로 하면 안되는데 해서, 잠깐 에러를 만났다.. d는 숫자가 아니고 알파벳이다..
  3. 다시 도미노를 쌓을 때는, 원래 가지고 있던 높이 만큼 쌓아줘야 하므로 그 높이를 origin_height에 copy.deepcopy를 이용해서 저장해뒀다.

👩‍💻 코드

import sys, collections, copy

input = sys.stdin.readline

N, M, R = map(int, input().split())
game_map = list(list(map(int, input().split())) for _ in range(N))
origin_height = copy.deepcopy(game_map)

attacker_score = 0

for _ in range(R):  # 라운드
    # 공격수의 행동
    x, y, d = input().split()
    domino = collections.deque()
    x, y = int(x) - 1, int(y) - 1

    domino.append((x, y, game_map[x][y]))
    while domino:
        i, j, height = domino.popleft()

        idx = 0
        while idx < height:

            if d == 'E':
                if j + idx < M:
                    if game_map[i][j + idx] != 0:
                        domino.append((i, j + idx, game_map[i][j + idx]))
                        attacker_score += 1
                        game_map[i][j + idx] = 0
                else:
                    break

            elif d == 'W':
                if 0 <= j - idx:
                    if game_map[i][j - idx] != 0:
                        domino.append((i, j - idx, game_map[i][j - idx]))
                        attacker_score += 1
                        game_map[i][j - idx] = 0
                else:
                    break

            elif d == 'S':
                if i + idx < N:
                    if game_map[i + idx][j] != 0:
                        domino.append((i + idx, j, game_map[i + idx][j]))
                        attacker_score += 1
                        game_map[i + idx][j] = 0
                else:
                    break
            elif d == 'N':
                if 0 <= i - idx:
                    if game_map[i - idx][j] != 0:
                        domino.append((i - idx, j, game_map[i - idx][j]))
                        attacker_score += 1
                        game_map[i - idx][j] = 0


                else:
                    break

            idx += 1

    # 수비수의 행동
    x1, y1 = map(int, input().split())
    x1 -= 1
    y1 -= 1
    game_map[x1][y1] = origin_height[x1][y1]

print(attacker_score)
for i in range(N):
    for j in range(M):
        if game_map[i][j] == 0:
            print('F', end=' ')
        else:
            print('S', end=' ')
    print()
728x90