프로그래머스

[프로그래머스] 행렬 테두리 회전하기(77485)

우엥우아앙 2021. 10. 13. 18:18

문제

https://programmers.co.kr/learn/courses/30/lessons/77485

 

코딩테스트 연습 - 행렬 테두리 회전하기

6 6 [[2,2,5,4],[3,3,6,6],[5,1,6,3]] [8, 10, 25] 3 3 [[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]] [1, 1, 5, 3]

programmers.co.kr

 

문제 접근

x1, y1 은 map을 1,1 부터 시작이라고 했을 때 위치의 값을 나타낸다라고 생각한다.

이렇게 생각하면 x2, y2 까지 칸을 이동한다라고 그림을 그릴 수 있다.

즉,

x1, y1 -> x1, y1+1 

x1, y1+1 -> x1, y1+2

x1, y1+2 -> x1, y1+3

..

이런식으로 시계방향으로 한 칸씩 이동을 할 수 있다.

여기서 x1, y1의 x축 내에서의 이동은 y2까지 이동할 수 있다라고 결론이 나온다.

 

또한, y2까지의 이동을 마치면, x2로의 이동이 시작되면서 

x1, y2 -> x1+1, y2

x1+1, y2 -> x1+2, y2

x1+2, y2 -> x1+3, y2

..

이런식으로 이동되며, 여기에서도 x2까지 이동을 하면 멈추게 되면서 시계방향으로 계속 이동 시켜주면 된다.

 

** 중요한 점 **

값 셋팅은 반대방향으로 이동시켜주면서 셋팅되어야 한다.

즉, x++ -> y++ -> x-- -> y-- 순이 될 것이다.

 

 

문제 풀이 코드

package D_2021_TopSession;

public class S_77485 {

	public static void main(String[] args) {
//		6	6	[[2,2,5,4],[3,3,6,6],[5,1,6,3]]	[8, 10, 25]
//		int rows=6;
//		int columns=6;
//		int[][] queries = {{2,2,5,4},{3,3,6,6},{5,1,6,3}};

//		3	3	[[1,1,2,2],[1,2,2,3],[2,1,3,2],[2,2,3,3]]	[1, 1, 5, 3]
//		int rows=3;
//		int columns=3;
//		int[][] queries = {{1,1,2,2},{1,2,2,3},{2,1,3,2},{2,2,3,3}};
		
		
//		100	97	[[1,1,100,97]]	[1]
		int rows=100;
		int columns=97;
		int[][] queries = {{1,1,100,97}};
		
		int[] answer = solution(rows, columns, queries);
		
		System.out.print("[");
		for(int rs : answer) {
			System.out.print(rs+", ");
		}
		System.out.println("]");
				
	}
	public static int[] solution(int rows, int columns, int[][] queries) {
        int[] answer = new int[queries.length];
        
        int[][] map = new int[rows+1][columns+1];
        int num=1;
        for(int i=1;i<=rows;i++) {
        	for(int j=1;j<=columns;j++) {
        		map[i][j] = num++; 
        	}
        }
        for(int i=0;i<queries.length;i++) {
        	int x1 = queries[i][0];
            int y1 = queries[i][1];
            int x2 = queries[i][2];
            int y2 = queries[i][3];

            int minCnt = num;
            
            int temp = map[x1][y1];
            int nowX = x1;
            int nowY = y1;
//            System.out.println("(start) ["+nowX+", "+nowY+"]");
            while(nowX<x2) {
            	map[nowX][nowY] = map[nowX+1][nowY];
            	minCnt = Math.min(minCnt, map[nowX][nowY]);
            	nowX++;
            }
//            System.out.println("(step1) ["+nowX+", "+nowY+"]");
            while(nowY<y2) {
            	map[nowX][nowY] = map[nowX][nowY+1];
            	minCnt = Math.min(minCnt, map[nowX][nowY]);
            	nowY++;
            }
//            System.out.println("(step2) ["+nowX+", "+nowY+"]");
            
            while(nowX>x1) {
            	map[nowX][nowY] = map[nowX-1][nowY];
            	minCnt = Math.min(minCnt, map[nowX][nowY]);
            	nowX--;
            }
//            System.out.println("(step3) ["+nowX+", "+nowY+"]");
            
            while(nowY>y1) {
            	if(nowX==x1 && nowY==y1+1) {
            		map[nowX][nowY] = temp;
                	minCnt = Math.min(minCnt, map[nowX][nowY]);
            		break;
            	}
            	map[nowX][nowY] = map[nowX][nowY-1];
            	minCnt = Math.min(minCnt, map[nowX][nowY]);
            	nowY--;
            }
//            System.out.println("(step4) ["+nowX+", "+nowY+"]");
            answer[i] = minCnt;
            
        }
//        printMap(map);
        return answer;
    }
	public static void printMap(int[][] map) {
		for(int i=1;i<map.length;i++) {
			for(int j=1;j<map[0].length;j++) {
				System.out.print(map[i][j]+" ");
			}
			System.out.println();
		}
	}
}