Skip to content

Commit 2e98b84

Browse files
committed
[Simulation] baekjoon-16927
1 parent 0a9bd93 commit 2e98b84

File tree

4 files changed

+88
-0
lines changed

4 files changed

+88
-0
lines changed

โ€ŽREADME.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
| 05 | | [Baekjoon-1244 ์Šค์œ„์น˜ ์ผœ๊ณ  ๋„๊ธฐ](./src/Simulation/P1244) | |
3535
| 06 | | [SWEA-1873 ์ƒํ˜ธ์˜ ๋ฐฐํ‹€ํ•„๋“œ](./src/Simulation/swea1873) | |
3636
| 07 | | [Baekjoon-2563 ์ƒ‰์ข…์ด](./src/Simulation/P2563) | |
37+
| 08 | | [Baekjoon-16927 ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2](./src/Simulation/P16927) | |
3738

3839
## String
3940

โ€Žsrc/Simulation/P16927/Main.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package Simulation.P16927;
2+
3+
import java.io.*;
4+
import java.util.StringTokenizer;
5+
6+
public class Main {
7+
8+
static int N, M, R;
9+
static int[][] origin, result;
10+
11+
static int[] di = {1, 0, -1, 0};
12+
static int[] dj = {0, 1, 0, -1};
13+
14+
public static void main(String[] args) throws Exception {
15+
System.setIn(new FileInputStream("src/Simulation/P16927/input.txt"));
16+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
17+
StringBuilder sb = new StringBuilder();
18+
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
N = Integer.parseInt(st.nextToken());
21+
M = Integer.parseInt(st.nextToken());
22+
R = Integer.parseInt(st.nextToken());
23+
24+
origin = new int[N][M];
25+
result = new int[N][M];
26+
27+
for (int i = 0; i < N; i++) {
28+
st = new StringTokenizer(br.readLine());
29+
for (int j = 0; j < M; j++) {
30+
origin[i][j] = Integer.parseInt(st.nextToken());
31+
}
32+
}
33+
34+
for (int s = 0; s < Math.min(N, M)/2; s++) {
35+
int n = N - s*2, m = M - s*2;
36+
int cnt = (n + m) * 2 - 4;
37+
int i = s, j = s, dir = 0, r = R % cnt;
38+
int[] pos = new int[cnt];
39+
for (int c = 0; c < cnt + r; c++) {
40+
if (c < cnt) pos[c] = origin[i][j];
41+
if (c >= r) result[i][j] = pos[c-r];
42+
if (!canGo(i + di[dir % 4], j + dj[dir % 4], s, n, m)) dir++;
43+
i += di[dir % 4];
44+
j += dj[dir % 4];
45+
}
46+
}
47+
48+
for (int i = 0; i < N; i++) {
49+
for (int j = 0; j < M; j++) {
50+
sb.append(result[i][j]).append(" ");
51+
}
52+
sb.append("\n");
53+
}
54+
55+
System.out.print(sb.toString());
56+
}
57+
58+
static boolean canGo(int i, int j, int s, int n, int m) {
59+
return s <= i && i < s+n && s <= j && j < s+m;
60+
}
61+
}

โ€Žsrc/Simulation/P16927/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
## [baekjoon-16927] ๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 2
2+
3+
- ๋™์ผํ•œ ๋ฌธ์ œ (์ฃผ์–ด์ง„ ๋ฒ”์œ„๋งŒ ๋‹ค๋ฆ„) : [๋ฐฐ์—ด ๋Œ๋ฆฌ๊ธฐ 1](https://www.acmicpc.net/problem/16926)
4+
5+
![image](https://user-images.githubusercontent.com/22045163/107479918-1e86ac00-6bbf-11eb-8dcc-a40ef1bb980b.png)
6+
7+
### ํ’€์ด ๊ณผ์ •
8+
9+
์—ญ์‹œ ์‹œ๋ฎฌ๋ ˆ์ด์…˜์€ ๋” ๋งŽ์ด ์—ฐ์Šตํ•ด์•ผ๊ฒ ๋‹ค. ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ๋ฐ”๋กœ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„ ์ƒ๊ฐ์„ ์ •๋ง ๋งŽ์ด ํ–ˆ๋‹ค. ํ’€์ด ๊ณผ์ •์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.
10+
11+
1. ๋ฐ”๊นฅ, ์•ˆ์ชฝ์„ ๋‚˜๋ˆ„์–ด ๊ฐ์ž ๋ฐ˜์‹œ๊ณ„ ๋ฐฉํ–ฅ์œผ๋กœ ํšŒ์ „ํ•˜๊ฒŒ ๋˜๋Š”๋ฐ, ์ด ๋‘˜๋ ˆ์˜ ์‹œ์ž‘ ์ธ๋ฑ์Šค์™€ ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ๋จผ์ € ๊ตฌํ–ˆ๋‹ค.
12+
๊ทœ์น™์„ ์ฐพ์•„ ๊ทธ ์ธ๋ฑ์Šค๋“ค์„ ๊ณ„์‚ฐํ•˜๊ณ  `s`, `n`, `m` ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์˜€๋‹ค.
13+
2. [๋‹ฌํŒฝ์ด ์ˆซ์ž](../../Array/swea1954) ๋ฌธ์ œ์ฒ˜๋Ÿผ ์‹œ์ž‘-๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค ๋ฒ”์œ„ ๋‚ด์—์„œ ํ•œ๋ฐ”ํ€ด ๋Œ๋„๋ก ๋งŒ๋“ค์—ˆ๋‹ค.
14+
์ด ๋•Œ, ํ•œ ๋ฐ”ํ€ด ๋„๋Š” ํšŸ์ˆ˜๋Š” `(n + m) * 2 - 4` ๋ฒˆ์ด๋‹ค. ์ด๋ฅผ `cnt` ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์˜€๋‹ค.
15+
3. ํšŒ์ „ ์ˆ˜๊ฐ€ ํ•œ ๋ฐ”ํ€ด ๋„๋Š” ํšŸ์ˆ˜๋ฅผ ๋„˜์–ด๊ฐˆ ๊ฒฝ์šฐ ํ•„์š” ์—†๋Š” ์—ฐ์‚ฐ์ด ๋ฐœ์ƒํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ฃผ์–ด์ง„ ํšŒ์ „ ์ˆ˜๋ฅผ `cnt` ๊ฐ’์œผ๋กœ mod ์—ฐ์‚ฐํ•ด์ค€๋‹ค.
16+
4. 0๋ถ€ํ„ฐ `cnt`๋ฒˆ๊นŒ์ง€ ๋ฒˆํ˜ธ๋ฅผ ๋งค๊ธฐ๋ฉฐ ํšŒ์ „ํ•˜๊ณ , ๊ทธ ๊ฐ’์„ ๋ฒˆํ˜ธ ๋ณ„๋กœ `pos` ๋ฐฐ์—ด์— ๋„ฃ๋Š”๋‹ค.
17+
5. `pos` ๋ฐฐ์—ด์˜ ๋ฒˆํ˜ธ์— ์•Œ๋งž๋Š” ๋ฐฐ์—ด ์œ„์น˜์— ๊ฐ’์„ ํ• ๋‹นํ•œ๋‹ค.
18+
19+
![image](https://user-images.githubusercontent.com/22045163/107479829-ff881a00-6bbe-11eb-8fb2-47a0d5bc2143.png)
20+
21+
![image](https://user-images.githubusercontent.com/22045163/107479984-35c59980-6bbf-11eb-90db-dcc64b906e76.png)

โ€Žsrc/Simulation/P16927/input.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
4 4 2
2+
1 2 3 4
3+
5 6 7 8
4+
9 10 11 12
5+
13 14 15 16

0 commit comments

Comments
ย (0)