카카오 페이 2번

이숭인·2021년 7월 13일
0

알고리즘 문제풀이

목록 보기
11/17
def solution(rows, columns, swipes):
    answer = []
    map = [[0]* columns for _ in range(rows)]

    for row in range(rows):
        for col in range(columns):
            map[row][col] = row * columns + col + 1

    for d,x1,y1,x2,y2 in swipes:
        x1 -= 1; y1 -= 1; x2 -= 1; y2 -= 1
        if d == 1:
            tmp = []
            for i in range(x2,x1,-1):
                for j in range(y1,y2+1):
                    if i == x2:
                        tmp.append(map[i][j])
                    map[i][j] = map[i-1][j]
            answer.append(sum(tmp))
            for j in range(y1,y2+1):
                map[x1][j] = tmp.pop(0)
        elif d == 2:
            tmp = []
            for i in range(x1,x2):
                for j in range(y1,y2+1):
                    if i == x1:
                        tmp.append(map[i][j])
                    map[i][j] = map[i+1][j]
            answer.append(sum(tmp))
            for j in range(y1,y2+1):
                map[x2][j] = tmp.pop(0)
        elif d == 3:
            tmp = []
            for j in range(y2,y1,-1):
                for i in range(x1,x2+1):
                    if j == y2:
                        tmp.append(map[i][j])
                    map[i][j] = map[i][j-1]
            answer.append(sum(tmp))
            for i in range(x1,x2+1):
                map[i][y1] = tmp.pop(0)
        elif d == 4:
            tmp = []
            for j in range(y1,y2):
                for i in range(x1,x2+1):#check
                    if j == y1:
                        tmp.append(map[i][j])
                    map[i][j] = map[i][j+1]
            answer.append(sum(tmp))
            for i in range(x1,x2+1):
                map[i][y2] = tmp.pop(0)

    return answer
# d 의 값이
# 1: 아래, 2: 위 , 3: 오른쪽, 4: 왼쪽
# r1,c1 -> r2,c2 까지의 숫자들을 d방향으로 스와이프
profile
iOS Developer

0개의 댓글