Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
Tags
- 백준 17070번 c++
- dfs
- 유니티
- Lv.3
- 코딩테스트
- c++
- 2870번 수학숙제 c++
- Beakjoon
- 플레이어 이동
- 2870번
- Unity
- 백준 17070번
- 2468 c++
- 백준 c++ 2870번
- 코테
- 2870번 c++
- 백준 2870번
- 백준 c++ 2468번
- 수학숙제
- 백준 1103번 게임
- 백준 1103번
- 17070번
- Algorithm
- 2870번 수학숙제
- C#
- 오브젝트 풀링
- Lv2
- 프로그래머스
- 백준 1103번 c++
- 백준
Archives
- Today
- Total
주녘공부일지
[프로그래머스 C#] Lv.2 행렬 테두리 회전하기 본문
https://school.programmers.co.kr/learn/courses/30/lessons/77485
1. 정답코드 및 핵심 아이디어, 유의사항
- 보통 가로축을 x, 세로축을 y로 보는 것이 익숙하기 때문에 헷갈릴 수 있음
-> 문제에서는 가로축 y, 세로축 x로 잡기 때문에 덜 헷갈리기 위해 통일해서 풀이
- 주어진 조건에 따른 직사각형 범위의 테두리에 해당하는 값을 회전시켜야 하므로 Queue를 이용함
- 조건에 따르면, 무조건 시작점 -> 도착점을 이루는 직사각형을 시계방향으로 회전시키기 때문에 한 사이클은 시작점에서 우측 -> 아래 -> 좌측 -> 위 방향을 한 사이클로 봐도 되며, 값을 저장하여 다음 값을 주는 것을 반복해야 하므로 Queue를 이용
https://godgjwnsgur7.tistory.com/46
주석 참조
using System;
using System.Collections.Generic;
public class Solution
{
public int[] solution(int rows, int columns, int[,] queries)
{
List<int> answerList = new List<int>();
Queue<int> queue = new Queue<int>();
// 주어진 문제 기준에 따라 세로축 x, 가로축 y로 잡음
int[,] intArrays = new int[rows + 1, columns + 1]; // 행렬
// 행렬 값 세팅
int num = 1;
for (int i = 1; i < intArrays.GetLength(0); i++)
{
for (int j = 1; j < intArrays.GetLength(1); j++)
{
intArrays[i, j] = num;
num++;
}
}
for (int i = 0; i < queries.GetLength(0); i++)
{
int currPosX = queries[i, 0];
int currPosY = queries[i, 1];
int minValue = intArrays[currPosX, currPosY];
queue.Enqueue(minValue);
// 1. 오른쪽 이동 (y++)
while (queries[i, 3] > currPosY)
{
currPosY++;
if (intArrays[currPosX, currPosY] < minValue)
minValue = intArrays[currPosX, currPosY];
queue.Enqueue(intArrays[currPosX, currPosY]);
intArrays[currPosX, currPosY] = queue.Dequeue();
}
// 2. 아래 이동 (x++)
while (queries[i, 2] > currPosX)
{
currPosX++;
if (intArrays[currPosX, currPosY] < minValue)
minValue = intArrays[currPosX, currPosY];
queue.Enqueue(intArrays[currPosX, currPosY]);
intArrays[currPosX, currPosY] = queue.Dequeue();
}
// 3. 왼쪽 이동 (y--)
while (queries[i, 1] < currPosY)
{
currPosY--;
if (intArrays[currPosX, currPosY] < minValue)
minValue = intArrays[currPosX, currPosY];
queue.Enqueue(intArrays[currPosX, currPosY]);
intArrays[currPosX, currPosY] = queue.Dequeue();
}
// 4. 오른쪽 이동 (x--)
while (queries[i, 0] < currPosX)
{
currPosX--;
if (intArrays[currPosX, currPosY] < minValue)
minValue = intArrays[currPosX, currPosY];
queue.Enqueue(intArrays[currPosX, currPosY]);
intArrays[currPosX, currPosY] = queue.Dequeue();
}
answerList.Add(minValue);
queue.Clear();
}
return answerList.ToArray();
}
}
'CodingTest > Programmers Lv.2' 카테고리의 다른 글
[프로그래머스 C#] Lv.2 요격 시스템 (0) | 2023.11.09 |
---|---|
[프로그래머스 C#] Lv.2 두 원 사이의 정수 쌍 (0) | 2023.11.07 |
[프로그래머스 C#] Lv.2 숫자 카드 나누기 (0) | 2023.10.31 |
[프로그래머스 C#] Lv.2 시소 짝꿍 (0) | 2023.10.13 |
[프로그래머스 C#] Lv.2 뒤에 있는 큰 수 찾기 (0) | 2023.09.18 |