728x90
반응형
SMALL
[프로그래머스] Lev 2. 행렬 테두리 회전하기
https://school.programmers.co.kr/learn/courses/30/lessons/77485


1시간 오버(좌표 로테이션에서 막힘), 그외는 모두 구현
걸린시간: 1시간 20분
import java.io.IOException;
import java.util.Arrays;
class Solution {
static int[][] map;
static boolean[][] mustChange;
public static int[] solution(int columns, int rows, int[][] queries) {
map = new int[columns][rows];
int[] answer = new int[queries.length];
int num = 1;
for (int i = 0; i <columns ; i++) {
for (int j = 0; j <rows ; j++) {
map[i][j]= num++;
}
}
for (int i = 0; i < queries.length ; i++) {
mustChange = new boolean[columns][rows];
int[] q = queries[i];
int col1= q[0]-1;
int row1= q[1]-1;
int col2= q[2]-1;
int row2= q[3]-1;
int min = findBoundary(col1, row1, col2, row2);
answer[i]= min;
int[][] rotateMap = rotate(map, col1, row1, col2, row2);
//2차원 깊은 복사
map = rotateMap;
}
return answer;
}
public static int findBoundary(int col1, int row1, int col2, int row2){
int min = Integer.MAX_VALUE;
//왼쪽
for (int i =col1; i <=col2 ; i++) {
min = Math.min(min, map[i][row1]);
mustChange[i][row1] = true;
}
//오른쪽
for (int i = col1; i <=col2 ; i++) {
min = Math.min(min, map[i][row2]);
mustChange[i][row2] = true;
}
//위
for (int i = row1+1; i <row2 ; i++) {
min = Math.min(min, map[col1][i]);
mustChange[col1][i] = true;
}
//아래
for (int i = row1+1; i <row2 ; i++) {
min = Math.min(min, map[col2][i]);
mustChange[col2][i] = true;
}
return min;
}
public static int[][] rotate(int[][] map, int x1, int y1, int x2, int y2){
int[][] swapMap = new int[map.length][map[0].length];
//2차원 깊은 복사
for (int i = 0; i < map.length ; i++) {
System.arraycopy(map[i], 0, swapMap[i], 0, swapMap[0].length);
}
for (int i = 0; i < map.length ; i++) {
for (int j = 0; j <map[0].length ; j++) {
//변경 대상
if (mustChange[i][j]){
//꼭지점 4개
// row++
if (i==x1 && j==y1){
swapMap[i][j+1] = map[i][j];
}
// col++
else if(i==x1 && j==y2){
swapMap[i+1][j] = map[i][j];
}
// row--
else if(i==x2 && j==y2){
swapMap[i][j-1] = map[i][j];
}
// col--
else if(i==x2 && j==y1){
swapMap[i-1][j] = map[i][j];
}
// 꼭지점을 제외한 4방향(왼,오,위,아래)
else {
int[] next = findSwapPoint(i,j,x1,y1,x2,y2);
swapMap[next[0]][next[1]] = map[i][j];
}
}
}
}
return swapMap;
}
public static int[] findSwapPoint(int col, int row, int x1, int y1, int x2, int y2){
//4방향
//왼
if (row==y1){
if (x1<col && col<x2){
return new int[]{col-1, row};
}
}
//오
if (row==y2){
if (x1<col && col<x2){
return new int[]{col+1, row};
}
}
//위
if (col==x1){
if (y1<row && row<y2){
return new int[]{col, row+1};
}
}
//아래
if (col==x2){
if (y1<row && row<y2){
return new int[]{col, row-1};
}
}
System.out.println(col+":"+row);
return null;
}
}
rorate 코드 리펙토링
public static int[][] rotateV1(int[][] map, int col1, int row1,int col2, int row2){
Queue<Integer> q = new ArrayDeque<>();
//위
q.add(map[col1][row1]);
for (int i=row1+1; i<row2 ; i++) {
q.add(map[col1][i]);
map[col1][i] = q.poll();
}
//오
for (int i=col1; i<col2 ; i++) {
q.add(map[i][row2]);
map[i][row2] = q.poll();
}
//아래
for (int i=row2; i>row1 ; i--) {
q.add(map[col2][i]);
map[col2][i] = q.poll();
}
//왼, 마지막에만 >=같다 표시를 해서 최초 지점 채워주기
for (int i=col2; i>=col1 ; i--) {
q.add(map[i][row1]);
map[i][row1] = q.poll();
}
return map;
}
728x90
반응형
LIST
'Personal Studying~ > 자바문제 풀어보기' 카테고리의 다른 글
| [프로그래머스] Lev 2. 괄호 회전하기 (1) | 2025.09.24 |
|---|---|
| [프로그래머스] Lev 2. 신고 결과 받기 (0) | 2025.09.24 |
| [프로그래머스] Lev 2. 성격 유형 검사하기 (0) | 2025.09.23 |
| [재귀, 백트래킹] 연산자 끼워넣기(BOJ 14888) (1) | 2025.05.15 |
| [프로그래머스]lev2 순위검색 (0) | 2023.09.20 |