https://www.acmicpc.net/problem/20165

이번 문제는 도미노를 넘기는 시뮬레이션을 통해 공격수가 총 몇 개의 도미노를 넘겼는지 계산하고, 최종 게임판의 상태를 출력하는 문제입니다. 공격과 수비의 과정을 반복하면서 도미노의 상태를 관리해야 합니다. 효율적이고 간결한 파이썬 코드를 작성하고, 그에 대한 자세한 분석을 제공하겠습니다.
예제 입력 1:
5 5 3
1 1 1 1 1
1 2 2 1 1
3 1 2 2 2
1 3 2 1 1
1 3 3 1 1
3 1 E
3 5
5 3 N
3 3
5 2 N
3 1
예제 출력 1:
11
S F S S S
S F S S S
S F S F S
S F F S S
S F F S S
해석:
이 문제는 시뮬레이션을 통해 각 라운드마다 도미노의 상태를 관리하고, 공격수의 점수를 계산하는 방식으로 해결할 수 있습니다. 주요 단계는 다음과 같습니다:
height에 저장합니다.toppled를 만듭니다.아래는 위의 접근 방식을 구현한 파이썬 코드입니다.
import sys
from collections import deque
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx])
M = int(data[idx+1])
R = int(data[idx+2])
idx += 3
# Read the grid heights
height = []
for _ in range(N):
row = list(map(int, data[idx:idx+M]))
height.append(row)
idx += M
# Initialize toppled status: False = 'S', True = 'F'
toppled = [[False for _ in range(M)] for _ in range(N)]
# Direction mappings
direction_map = {
'N': (-1, 0),
'S': (1, 0),
'E': (0, 1),
'W': (0, -1)
}
total_score = 0
# Process R rounds
for _ in range(R):
# Attack action
attack_x = int(data[idx]) -1
attack_y = int(data[idx+1]) -1
D = data[idx+2]
idx +=3
# Perform attack if the domino is standing
if not toppled[attack_x][attack_y]:
queue = deque()
queue.append( (attack_x, attack_y, D) )
toppled[attack_x][attack_y] = True
total_score +=1
while queue:
x, y, direction = queue.popleft()
K = height[x][y]
dx, dy = direction_map[direction]
for step in range(1, K):
nx = x + dx*step
ny = y + dy*step
if 0 <= nx < N and 0 <= ny < M:
if not toppled[nx][ny]:
toppled[nx][ny] = True
total_score +=1
# If the domino has height >1, it can topple further
if height[nx][ny] >1:
queue.append( (nx, ny, direction) )
else:
break # Out of bounds
# Defense action
defend_x = int(data[idx]) -1
defend_y = int(data[idx+1]) -1
idx +=2
# Perform defense if the domino is toppled
if toppled[defend_x][defend_y]:
toppled[defend_x][defend_y] = False
# Output
print(total_score)
for row in toppled:
print(' '.join(['F' if cell else 'S' for cell in row]))
if __name__ == "__main__":
main()
import sys
from collections import deque
def main():
import sys
input = sys.stdin.read
data = input().split()
idx = 0
N = int(data[idx])
M = int(data[idx+1])
R = int(data[idx+2])
idx += 3
sys.stdin.read()를 사용하여 전체 입력을 한 번에 읽어옵니다.split()을 통해 입력을 공백 기준으로 분할하여 data 리스트에 저장합니다.idx를 사용하여 입력 데이터의 현재 위치를 추적합니다. # Read the grid heights
height = []
for _ in range(N):
row = list(map(int, data[idx:idx+M]))
height.append(row)
idx += M
# Initialize toppled status: False = 'S', True = 'F'
toppled = [[False for _ in range(M)] for _ in range(N)]
height 2차원 리스트에 저장합니다.toppled 2차원 리스트를 초기화하여 모든 도미노를 세운 상태('S')로 설정합니다. False는 'S', True는 'F'를 의미합니다. # Direction mappings
direction_map = {
'N': (-1, 0),
'S': (1, 0),
'E': (0, 1),
'W': (0, -1)
}
total_score = 0
# Process R rounds
for _ in range(R):
# Attack action
attack_x = int(data[idx]) -1
attack_y = int(data[idx+1]) -1
D = data[idx+2]
idx +=3
# Perform attack if the domino is standing
if not toppled[attack_x][attack_y]:
queue = deque()
queue.append( (attack_x, attack_y, D) )
toppled[attack_x][attack_y] = True
total_score +=1
while queue:
x, y, direction = queue.popleft()
K = height[x][y]
dx, dy = direction_map[direction]
for step in range(1, K):
nx = x + dx*step
ny = y + dy*step
if 0 <= nx < N and 0 <= ny < M:
if not toppled[nx][ny]:
toppled[nx][ny] = True
total_score +=1
# If the domino has height >1, it can topple further
if height[nx][ny] >1:
queue.append( (nx, ny, direction) )
else:
break # Out of bounds
X, Y, D.1).total_score를 1 증가시킵니다.K만큼 지정된 방향으로 도미노를 넘깁니다.total_score를 증가시킵니다. # Defense action
defend_x = int(data[idx]) -1
defend_y = int(data[idx+1]) -1
idx +=2
# Perform defense if the domino is toppled
if toppled[defend_x][defend_y]:
toppled[defend_x][defend_y] = False
X, Y.1). # Output
print(total_score)
for row in toppled:
print(' '.join(['F' if cell else 'S' for cell in row]))
O(N*M + R)O(R*K)O(N*M + R)O(N*M)O(N*M)O(N*M) (최악의 경우 모든 도미노가 동시에 넘어질 수 있지만, N,M이 작으므로 무시 가능)O(N*M)height와 toppled):height 리스트와 도미노의 현재 상태를 저장하는 toppled 리스트를 사용합니다.deque):collections.deque를 사용합니다.