programmers_행렬 테두리 회전하기_java
2023. 4. 22. 17:37ㆍAlgorithm/Programmers
728x90
https://school.programmers.co.kr/learn/courses/30/lessons/77485
class Solution {
private static int delta[][] = {{0,1}, {1, 0}, {0, -1}, {-1, 0}}; // 우, 하, 좌, 상
public int[] solution(int rows, int columns, int[][] queries) {
int queriesLength = queries.length;
int[] answer = new int[queriesLength];
int[][] map = new int[rows+1][columns+1];
int cnt = 1;
for(int i = 1; i<= rows ; i++){
for(int j = 1; j<= columns; j++){
map[i][j] = cnt++;
}
}
for(int i = 0; i<queriesLength; i++){
answer[i] = move(map, rows, columns, queries[i][0], queries[i][1], queries[i][2], queries[i][3]);
}
return answer;
}
public int move(int[][] map, int rows, int columns, int sy, int sx, int ey, int ex){
int min = map[sy][sx];
int tmp = map[sy][sx];
int y = sy, x = sx;
int dir = 0;
while(dir<4){
int nexty = y + delta[dir][0];
int nextx = x + delta[dir][1];
if(sy<= nexty && nexty <=ey && sx<= nextx && nextx <=ex){
int t = map[nexty][nextx];
map[nexty][nextx] = tmp;
tmp = t;
min = Math.min(min, map[nexty][nextx]);
y = nexty;
x = nextx;
}else{
dir++;
}
}
return min;
}
}
'Algorithm > Programmers' 카테고리의 다른 글
programmers_연속된 부분 수열의 합_java (0) | 2023.04.28 |
---|---|
programmers_[3차] 방금그곡_java (0) | 2023.04.26 |
programmers_괄호 변환_java (0) | 2023.04.22 |
programmers_택배상자_java (0) | 2023.04.17 |
programmers_두 큐 합 같게 만들기_java (0) | 2023.04.17 |