https://www.acmicpc.net/problem/16926
ํ๋ฉด์ ํ๋๋ ๋ฌธ์ ์ด๋ค..
ํ์ ์๋งํผ for๋ฌธ ์ ๋๋ ค,
์์์ ← , ↓, →, ↑ ๋ค ๋ฐฉํฅ์ผ๋ก ๋๋ ๋ฐฐ์ด์ ๊ฐ์ ๋ฐฉํฅ๋๋ก ์ฎ๊ธฐ๊ธฐ๋ก ํ๋ค.
์ธ๋ฑ์ค ๋๋ฌธ์ ์ ๋ฅผ ๋จน์๋ค. ํ๋ฆฐํธ๋ฌธ์ ์ฐ์ด๊ฐ๋ฉฐ ๊ฒฐ๊ณผ๊ฐ์ ํ์ธํ๋ฉฐ ์กฐ๊ธ์ฉ ๊ณ ์น ๋์ ์ ๋ต์ ๋ผ ์ ์์๋ค.
์คํจ ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[][] arr;
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());
int R = 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());
}
}
//๊ตฌํ
for(int r = 0; r < R; r++) {
rotate();
}
//์ถ๋ ฅ
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
public static void rotate() {
for(int i = 0; i < N / 2; i++) { //ํ์ ๋ฌถ์๋งํผ ๋ฐ๋ณต
int temp1 = arr[i][M - 1 - i];
int temp2 = 0;
for(int j = M - 2 - i; j >= i; j--) { //← ๋ฐฉํฅ
temp2 = arr[i][j];
arr[i][j] = temp1;
temp1 = temp2;
}
for(int j = i + 1; j < N - i; j++) { //↓ ๋ฐฉํฅ
temp2 = arr[j][i];
arr[j][i] = temp1;
temp1 = temp2;
}
for(int j = i + 1; j < M - i; j++) { //→ ๋ฐฉํฅ
temp2 = arr[N - 1 - i][j];
arr[N - 1 - i][j] = temp1;
temp1 = temp2;
}
for(int j = N - 2 - i; j >= i; j--) { //↑ ๋ฐฉํฅ
temp2 = arr[j][M - 1 - i];
arr[j][M - 1 - i] = temp1;
temp1 = temp2;
}
}
}
}
ํ์ ์๋งํผ ๋ฐ๋ณตํด์ฃผ๋ ๊ฒ์ N/2๋ก ํ๋๋ฐ, ๋ฌด์กฐ๊ฑด N์ด ์๋๋ผ M๊ณผ N ์ค์ ์์ ๊ฐ์ ์จ์ผ ํ๋ค.
์ฑ๊ณต ์ฝ๋
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
static int N, M;
static int[][] arr;
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());
int R = 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());
}
}
//๊ตฌํ
for(int r = 0; r < R; r++) {
rotate();
}
//์ถ๋ ฅ
StringBuilder sb = new StringBuilder();
for(int i = 0; i < N; i++) {
for(int j = 0; j < M; j++) {
sb.append(arr[i][j] + " ");
}
sb.append("\n");
}
System.out.println(sb);
}
public static void rotate() {
int d = Math.min(N, M);
for(int i = 0; i < d / 2; i++) { //ํ์ ๋ฌถ์๋งํผ ๋ฐ๋ณต
int temp1 = arr[i][M - 1 - i];
int temp2 = 0;
for(int j = M - 2 - i; j >= i; j--) { //← ๋ฐฉํฅ
temp2 = arr[i][j];
arr[i][j] = temp1;
temp1 = temp2;
}
for(int j = i + 1; j < N - i; j++) { //↓ ๋ฐฉํฅ
temp2 = arr[j][i];
arr[j][i] = temp1;
temp1 = temp2;
}
for(int j = i + 1; j < M - i; j++) { //→ ๋ฐฉํฅ
temp2 = arr[N - 1 - i][j];
arr[N - 1 - i][j] = temp1;
temp1 = temp2;
}
for(int j = N - 2 - i; j >= i; j--) { //↑ ๋ฐฉํฅ
temp2 = arr[j][M - 1 - i];
arr[j][M - 1 - i] = temp1;
temp1 = temp2;
}
}
}
}
๊ฒฐ๊ณผ
์๋๋ ์ถ๋ ฅ์ System.out.println()์ผ๋ก ํ ๊ฒ์ด๊ณ , ์์ ๊ฒ์ StringBuilder๋ฅผ ์ด์ฉํด ์ถ๋ ฅํ ๊ฒ์ด๋ค.
์ ๋ ฅ๊ฐ์ด ํฌ๋ฏ๋ก StringBuilder๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์๊ฐ์ ์ค์ด๋ ๋ฐ ์ ์ฉํ๋ค.
'Algorithm > ๋ฐฑ์ค' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋ฐฑ์ค 3040๋ฒ - ๋ฐฑ์ค ๊ณต์ฃผ์ ์ผ๊ณฑ ๋์์ด (0) | 2022.08.11 |
---|---|
๋ฐฑ์ค 16922๋ฒ - ๋ก๋ง ์ซ์ ๋ง๋ค๊ธฐ (0) | 2022.08.11 |
2304๋ฒ - ์ฐฝ๊ณ ๋ค๊ฐํ (0) | 2022.08.09 |
๋ฐฑ์ค 1158๋ฒ - ์์ธํธ์ค ๋ฌธ์ (0) | 2022.08.08 |
๋ฐฑ์ค 2630๋ฒ - ์์ข ์ด ๋ง๋ค๊ธฐ (0) | 2022.08.07 |