문제
Martin is attending a lecture on linear algebra. It is needless to say that the professor who is giving the lecture is the most boring person in the entire universe. There is a matrix written on the blackboard. Some of the entries in the matrix are letters (of the English alphabet) while some other entries are blank. Here is an example of such a matrix of size :
Martin has absolutely no idea what this matrix represents. He is so bored that he has not been following the lecture anymore for the last minutes. However, Martin has an extremely vivid imagination. He is imagining that the matrix is suddenly influenced by gravity and all the letters in it are sliding downwards until each letter either ‘reaches the bottom’ or ‘hits the letter that is below it’. In the first phase, the above matrix becomes:
After that, gravity changes direction and is now pulling the letters to the left. We are now in the second phase. Again, all the letters are sliding to the left until each letter either ‘reaches the left bracket’ or ‘hits the letter on its left’. The previous matrix thus becomes:
Martin is carrying out this procedure in his head until the very end of the boring lecture. Of course, after each phase, i.e. after all the letters land at their respective destinations, gravity may change its direction (there are four possibilities for the direction: left, right, up and down).
Write a program that determines the final positions of all letters in the matrix given the precise sequence of the gravity direction changes.
입력
The first line contains three integers , and where is the size of the matrix and is the number of phases.
The second line contains a string of length that consists of letters L, R, U and D that represent the direction of gravity in each phase (left, right, up and down, respectively).
The final lines represent the matrix. Each of the lines contains characters. The characters are lowercase letters of the English alphabet and ‘.’ (dot) which represents a blank entry.
출력
Output the matrix which Martin obtained at the end of the lecture. The format of the matrix is identic to the one in the input data.
풀이 과정
Up, Left, Right, Down 에 따라 해당 행이나 열에 있는 문자들을 모두 모아 밀면 되는 문제이다.
반복문과 인덱스 활용에 주의하자.
import sys
input = sys.stdin.readline
n, m, k = map(int, input().rstrip().split())
order = list(input().rstrip())
maps = [list(input().rstrip()) for _ in range(n)]
def up():
for j in range(m):
now = []
for i in range(n):
if maps[i][j] != '.': now.append(maps[i][j])
for i in range(n):
if i < len(now):
maps[i][j] = now[i]
else:
maps[i][j] = '.'
def down():
for j in range(m):
now = []
for i in range(n-1, -1, -1):
if maps[i][j] != '.': now.append(maps[i][j])
for i in range(n-len(now)):
now.append('.')
for i in range(n-1, -1, -1):
maps[i][j] = now[(n-1)-i]
def left():
for i in range(n):
now = []
for j in range(m):
if maps[i][j] != '.': now.append(maps[i][j])
for j in range(m-len(now)): now.append('.')
maps[i] = now
def right():
for i in range(n):
now = []
now = []
for j in range(m):
if maps[i][j] != '.': now.append(maps[i][j])
for j in range(m-len(now)): now = ['.'] + now
maps[i] = now
for o in order:
if o == 'U': up()
elif o == 'D': down()
elif o == 'L': left()
elif o == 'R': right()
for i in range(n): print(''.join(maps[i]))
'-- 예전 기록 > BOJ' 카테고리의 다른 글
[ BOJ ] 16458 : 가장 큰 숫자 ( GOLD 5 ) / Python (0) | 2023.07.08 |
---|---|
[ BOJ ] 10172 : 개 ( BRONZE 5 ) / C, C++, Python, Java (0) | 2023.07.08 |
[ BOJ ] 10171 : 고양이 ( BRONZE 5 ) / C, C++, Python, Java (0) | 2023.07.05 |
[ BOJ ] 11382 : 꼬마 정민 ( BRONZE 5 ) / C, C++, Python, Java (0) | 2023.07.04 |
[ BOJ ] 13909 : 창문 닫기 ( SILVER 5 ) / Python (0) | 2023.07.03 |