[백준] 17276번 배열 돌리기 (java, 구현)
728x90
반응형
대각선 45도, -45도로 배열을 돌리는 문제. 주 대각선들 외에는 그대로 냅둔다.
import java.util.Scanner;
public class Main {
static int n,t,d;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
for (int tc = 1; tc <=t; tc++) {
n=sc.nextInt();
d=sc.nextInt();
int[][] map=new int[n][n];
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
map[i][j]=sc.nextInt();
}
}
int[][] copy=copiedMap(map);
if(d>0) {//시계 방향
rotateR(copy);//시계 반시계 방향 결정해야함
}else {
rotateL(copy);
}
}
}
private static void rotateL(int[][] map) {
int[][] copy=copiedMap(map);
for (int cnt = 0; cnt < Math.abs(d)/45; cnt++) {
for (int i = 0; i < n; i++) {
copy[(n+1)/2-1][i]=map[i][i];
}
for (int i = 0; i < n; i++) {
copy[i][i]=map[i][(n+1)/2-1];
}
for (int i = 0; i < n; i++) {
copy[i][(n+1)/2-1]=map[i][n-1-i];
}
for (int i = 0; i < n; i++) {
copy[n-1-i][i]=map[(n+1)/2-1][i];
}
map=copiedMap(copy);
}
print(copy);
}
private static void rotateR(int[][] map) {
int[][] copy=copiedMap(map);
for (int cnt = 0; cnt < Math.abs(d)/45; cnt++) {
for (int i = 0; i < n; i++) {
copy[i][(n+1)/2-1]=map[i][i];
}
for (int i = 0; i < n; i++) {
copy[i][n-1-i]=map[i][(n+1)/2-1];
}
for (int i = 0; i < n; i++) {
copy[(n+1)/2-1][n-1-i]=map[i][n-1-i];
}
for (int i = 0; i < n; i++) {
copy[i][i]=map[(n+1)/2-1][i];
}
map=copiedMap(copy);
}
print(copy);
}
private static void print(int[][] map) {
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(map[i][j]+" ");
}
System.out.println();
}
}
private static int[][] copiedMap(int[][] map){
int[][] copy=new int[n][n];
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
copy[i][j]=map[i][j];
}
}
return copy;
}
}
728x90
반응형
'백준' 카테고리의 다른 글
[백준] 17471번 게리맨더링 (java, bfs, subset) (0) | 2021.04.16 |
---|---|
[백준] 3055번 탈출 (java, bfs) (0) | 2021.04.16 |
[백준] 19236번 청소년 상어 (java, 시뮬레이션) (0) | 2021.04.16 |
[백준] 20058번 마법사 상어와 파이어 스톰 (java, 시뮬레이션) (0) | 2021.04.16 |
[백준] 16234번 인구 이동 (bfs, 시뮬레이션, java) (0) | 2021.04.15 |
TAGS.