[SW Expert Academy] D3 1220번 [S/W 문제해결 기본] 5일차 - Magnetic(python)

good_da22·2022년 5월 20일
0

SW Expert Academy

목록 보기
18/20
post-thumbnail

SW Expert Academy

D3 1220번 [S/W 문제해결 기본] 5일차 - Magnetic(python)

문제

풀이과정

정해진 규칙에 따라 테이블 내에 더 이상의 이동이 불가할 경우(교착상태 도달)할 때 까지
정해진 규칙에 따라 이동해야 한다.

이동의 조건을 명시
이동이 발생할 경우 move = True
더 이상 이동이 발생하지 않을 경우 movep = False 이동 종료, 이후 교착 상태 확인

교착상태 확인
n극과 s극이 맞닿은 상태
n극과 s극이 맞닿은 상태의 개수 == 교착상태의 개수

소스코드

T = 10

for t in range(1, T+1):
    table_length = int(input())
    table = []

    for _ in range(table_length):
        table.append(list(map(int, input().split())))

    while True:
        move = False
        for i in range(table_length):
            for j in range(table_length):
                if table[i][j] == 1 and i == table_length-1: #n극이 아래로가서 떨어지는 경우
                    table[i][j] = 0
                    move = True
                if table[i][j] == 2 and i == 0: #s극이 위로가서 떨어지는 경우
                    table[i][j] = 0
                    move = True
                if table[i][j] == 1 and table[i+1][j] == 0: #n극이 아래로
                    table[i][j] = 0
                    table[i+1][j] = 1
                    move = True
                if table[i][j] == 2 and table[i-1][j] == 0: #s극이 위로
                    table[i][j] = 0
                    table[i-1][j] = 2
                    move = True

        if not move:
            break

    count = 0
    for i in range(table_length):
        for j in range(table_length-1):
            if table[j][i] == 1 and table[j+1][i] == 2:
                count += 1

    print("#{} {}".format(t, count))
profile
dev blog

0개의 댓글