본문 바로가기
python-algorithm

백준 9455 박스

by 무적김두칠 2022. 10. 29.

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

 

9455번: 박스

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 각 테스트 케이스의 첫째 줄에는 m과 n이 주어진다. (1 ≤ m, n ≤ 100) 다음 m개 줄에는 그리드의 각 행의 정보를 나타내는 n개의 정수가 주어진다. 그

www.acmicpc.net

 

1
2
3
4
5
6
7
8
9
10
11
t=int(input())
for _ in range(t):
    m,n=map(int,input().split())
    matrix=[list(map(int, input().split())) for i in range(m)]
    transposed_matrix=list(zip(*matrix))
    sum_distance_box=0
    for i in transposed_matrix:
        for j in range(1,m):
            if i[::-1][j]==1:
                sum_distance_box+=i[::-1][:j].count(0)
    print(sum_distance_box)
cs

행렬로 보면 transpose해서 박스 앞 즉 list의 값이 1 인경우 그 앞의 0의 개수를 전부 세서 더하면 움직인 거리

If this is matrix, Transpose it
if the value of the list is 1, the distance moved by counting all the 0s in front of it


numpy 라이브러리를 안쓰고 matrix를 transpose 하려고 Line 5 처럼 구현했지만
transposed 된 행렬의 행 내부 순서가 반대라서 Line9, Line10처럼 [::-1]와 같이 구현

In this circumstances, Not allowed to use numpy library
To transpose matrix , I did it like line 5
But rows in transposed matrix is reversed
So like line9,10 using [::-1] reverse rows

반응형

'python-algorithm' 카테고리의 다른 글

백준 12606 Reverse Words (Large)  (0) 2022.10.30
백준 13322 접두사 배열  (0) 2022.10.29
백준 2456 나는 학급회장이다  (0) 2022.10.29
백준 4641 Doubles  (0) 2022.10.29
백준 13235 팰린드롬  (0) 2022.10.28

댓글