프로그래머스 - 행렬 테두리 회전하기 (C#)

Leedong·2022년 7월 9일
0

programmers

목록 보기
16/18

문제 설명

행의 개수 rows, 열의 개수 columns, 회전들의 목록 queries가 주어질 때,
각 회전들을 배열에 적용한 뒤,
각 회전에 위치가 바뀐 숫자들 중 최솟값을 배열에 담아 반환하는 문제입니다.

문제 풀이

row가 2 columns 4일때,
1 2 3 4
5 6 7 8
다음과 같은 2차원 배열을 만들어야 합니다.

queries를 사용할 때 주의할 점은 생성한 배열의 인덱스 값을 맞추려면 각 요소에 -1을 해야 합니다.

(시계 방향으로 1만큼 회전 -> 회전한 숫자 중 가장 낮은 수를 최솟값 배열에 담기)를
queries 크기만큼 반복합니다.

enum을 이용해 방향을 정하면 어렵지 않게 회전할 수 있습니다.

제출 코드

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

public class Solution 
{
    public enum Direction
    {
        T, D, L, R
    }
    
    public int[] solution(int rows, int columns, int[,] queries) 
    {                
        int[,] matrix = new int[rows, columns];
        
        for (int i = 0, number = 1; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                matrix[i, j] = number++;
            }
        }
        
        int[] answer = new int[queries.GetLength(0)];
        
        for (int i = 0; i < queries.GetLength(0); i++)
        {
            int x1 = queries[i, 0] - 1, y1 = queries[i, 1] - 1;
            int x2 = queries[i, 2] - 1, y2 = queries[i, 3] - 1;
            
            int a = x1, b = y1;
            
            List<int> rotNumbers = new List<int>();
            rotNumbers.Add(matrix[a, b]);
            
            Queue<int> queue = new Queue<int>();
            queue.Enqueue(matrix[a, b]);
            
            Direction direction = Direction.R;
            
            b++;
            while (true)
            {
                rotNumbers.Add(matrix[a, b]);
                queue.Enqueue(matrix[a, b]); // 현재 값
                matrix[a, b] = queue.Dequeue(); // 이전 값
                
                if (a == x1 && b == y1)
                {
                    break;
                }
                
                switch (direction)
                {
                    case Direction.T :
                        a--;
                        break;
                    case Direction.D :
                        if (a == x2)
                        {
                            direction = Direction.L;
                            b--;                            
                        }                            
                        else
                            a++;
                        break;
                    case Direction.L :
                        if (b == y1)
                        {
                            direction = Direction.T;
                            a--;
                        }                            
                        else
                            b--;
                        break;
                    case Direction.R :
                        if (b == y2)
                        {
                            direction = Direction.D;
                            a++;
                        }                            
                        else
                            b++;                        
                        break;
                }
            }
            
            answer[i] = rotNumbers.Min();
        }        
        
        return answer;
    }
}
profile
Unity, C#

0개의 댓글