Coding - Algo/Java
[백준] 16926번:배열돌리기1 (Java 자바)
jainn
2021. 8. 11. 09:53
728x90
문제
https://www.acmicpc.net/problem/16926
16926번: 배열 돌리기 1
크기가 N×M인 배열이 있을 때, 배열을 돌려보려고 한다. 배열은 다음과 같이 반시계 방향으로 돌려야 한다. A[1][1] ← A[1][2] ← A[1][3] ← A[1][4] ← A[1][5] ↓ ↑ A[2][1] A[2][2] ← A[2][3] ← A[2][4] A[2][5]
www.acmicpc.net
풀이 및 소스코드
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int r = Integer.parseInt(st.nextToken());
int[][] map = new int[n][m];
for(int i=0;i<n;i++) {
st = new StringTokenizer(br.readLine());
for(int j=0;j<m;j++) {
map[i][j] = Integer.parseInt(st.nextToken());
}
}
int rota = Math.min(m, n)/2;
int[] dx = {0, 1, 0, -1};
int[] dy = {1, 0, -1, 0};
for(int rr=0;rr<r;rr++) {//회전의갯수
for(int ro=0;ro<rota;ro++) {//1회전 당 돌려줘야하는 구역?들의 갯수
int x = ro;
int y = ro;
int tmp = map[x][y];
for(int i=0;i<4;i++) {
int rep = i%2==0? m-1-(ro*2):n-1-(ro*2);
for(int j=0;j<rep;j++) {
int nowx = x+dx[i];
int nowy = y+dy[i];
if((0<=nowx&&nowx<n)&&(0<=nowy&&nowy<m)) {
map[x][y] = map[nowx][nowy];
x = nowx;
y = nowy;
}
}
}
map[ro+1][ro] = tmp;
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
}
반응형