-
[java] 백준 16926 : 배열 돌리기 1알고리즘 2023. 7. 27. 12:52
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.*; public class Main { static int[][] arr; static int min,n,m,cnt; static int[] dx = {0,1,0,-1}; static int[] dy = {1,0,-1,0}; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); n = Integer.parseInt(st.nextToken()); m = Integer.parseInt(st.nextToken()); cnt = Integer.parseInt(st.nextToken()); arr = new int[n][m]; for(int i=0; i<n; i++) { st = new StringTokenizer(br.readLine()); for(int j=0; j<m; j++) { arr[i][j] = Integer.parseInt(st.nextToken()); } } min = Math.min(n, m); for(int i=1; i<=cnt; i++) { rotate(); } for(int i=0; i<n; i++) { for(int j=0; j<m; j++) { System.out.print(arr[i][j]+" "); } System.out.println(); } } static void rotate() { for(int t=0; t<min/2; t++ ) { //회전 시킬 그룹의 갯수 int x = t; int y = t; int temp = arr[x][y]; //마지막에 넣을 ㄹ값 int idx =0; while(idx <4) { int nx = x + dx[idx]; int ny = y + dy[idx]; if(nx < n-t && ny <m-t && nx >= t && ny >=t) { arr[x][y] = arr[nx][ny]; x = nx; y = ny; }else { idx++; } } arr[t+1][t]= temp; } } }
참고
'알고리즘' 카테고리의 다른 글
프로그래머스 게임 맵 최단거리 (0) 2023.08.05 [java] 백준 16935 : 배열 돌리기 3 (0) 2023.07.30 [java] 프로그래머스 예상 대진표 (0) 2023.07.11 [java] 프로그래머스 키패드 누르기 (0) 2023.07.06 [java] 프로그래머스 큰 수 만들기 (0) 2023.07.06