# 2021 Dev-Matching - 행렬 테두리 회전하기

- Author: @mildsalmon
- Published: 2021-10-11
- Updated: 2021-10-16
- Source: http://blex.me/@mildsalmon/%ED%94%84%EB%A1%9C%EA%B7%B8%EB%9E%98%EB%A8%B8%EC%8A%A4-2021-dev-matching-%ED%96%89%EB%A0%AC-%ED%85%8C%EB%91%90%EB%A6%AC-%ED%9A%8C%EC%A0%84%ED%95%98%EA%B8%B0
- Tags: 파이썬, 알고리즘, 프로그래머스, 코딩테스트, 문제, dev, matching

---

# 1. 행렬 테두리 회전하기

- 난이도
	- Level 2
- 출처
	- [코딩테스트 연습 - 행렬 테두리 회전하기 | 프로그래머스 (programmers.co.kr)](https://programmers.co.kr/learn/courses/30/lessons/77485)

### A. 📜 문제

위 프로그래머스 사이트에 접속하여 문제를 확인해주세요.

### B. 💡 내 답안

##### a. 😊 1차 시도 (성공)

```python

def solution(rows, columns, queries):
    array = []
    
    for i in range(rows):
        temp = []
        for j in range(columns):
            temp.append(i * columns + (j+1))
        array.append(temp)
    
    answer = []
    
    for i in queries:
        x_1, y_1, x_2, y_2 = i
        row_len = x_2 - x_1
        column_len = y_2 - y_1
        dx, dy = x_1-1, y_1-1
        
        temp = [array[dx][dy]]
        # temp = []
        
        for j in range(column_len):
            dy += 1
            temp.append(array[dx][dy])
            array[dx][dy] = temp[-2]
        
        for j in range(row_len):
            dx += 1
            temp.append(array[dx][dy])
            array[dx][dy] = temp[-2]
            
        for j in range(column_len):
            dy -= 1
            temp.append(array[dx][dy])
            array[dx][dy] = temp[-2]
        
        for j in range(row_len):
            dx -= 1
            temp.append(array[dx][dy])
            array[dx][dy] = temp[-2]
        
        # print(temp)
        answer.append(min(temp))
        
        
    return answer

```

##### a. 🙄 회고

> 내 풀이

- 이런 비슷한 문제를 한번에 풀려다 실패한 기억이 있었다.
- 그래서 한 줄 한줄 차근차근 풀기로 했다.
- 직사각형의 위쪽 행, 오른쪽 열, 아래쪽 행, 왼쪽 열 순서로 값을 구해 리스트에 넣어서 풀었다.

> 반성

- 어떻게 하면 한 번에 풀 수 있을지, 고민이 조금 길었다.

### C. 🧐 문제 해설

> 이해한 내용을 바탕으로 작성했습니다.

![](https://static.blex.me/images/content/2021/10/11/2021_10_11_21_0lYTUsufpE8kXJSnTOCU.jpg)

![](https://static.blex.me/images/content/2021/10/11/2021_10_11_21_CmO5sRT62zMh0vhOYFgY.jpg)

단순히 풀면 temp 리스트에 값을 집어 넣고 값을 집어 넣을 때 사용했던 코드를 반복하면서 값을 교체하면 된다.

그런데, 그냥 그렇게 하기 싫었다. 복사 붙여넣기가 귀찮아서, 머리를 조금 더 굴려봤다.

# 참고문헌

2021 Dev-Matching_웹 백엔드 개발자(상반기).[코딩테스트 연습 - 행렬 테두리 회전하기 | 프로그래머스 (programmers.co.kr)](https://programmers.co.kr/learn/courses/30/lessons/77485). Programmers. (accessed Oct 11, 2021)
