[백준] 17276번 배열 돌리기 (java, 구현)

728x90
반응형

 

www.acmicpc.net/problem/17276

 

17276번: 배열 돌리기

각 테스트 케이스에 대해 회전 연산을 마친 후 배열의 상태를 출력한다. n줄에 걸쳐 각 줄에 n개의 정수를 공백으로 구분하여 출력한다. 

www.acmicpc.net

대각선 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
반응형
TAGS.

Comments