Skip to content

Commit fbf323e

Browse files
committed
[Simulation] baekjoon-21608
1 parent 4bd4d0e commit fbf323e

File tree

4 files changed

+128
-1
lines changed

4 files changed

+128
-1
lines changed

โ€ŽREADME.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
| 30 | | [SWEA-9760 Poker Game](./src/Simulation/swea9760) | |
7070
| 31 | | [SWEA-2115 ๋ฒŒ๊ฟ€์ฑ„์ทจ](./src/Simulation/swea2115) | |
7171
| 32 | | [Baekjoon-15686 ์น˜ํ‚จ ๋ฐฐ๋‹ฌ](./src/Simulation/P15686) | |
72+
| 33 | | [Baekjoon-21608 ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต](./src/Simulation/P21608) | |
7273

7374
## String
7475

@@ -519,4 +520,4 @@
519520
| 02 | | [์ˆ˜์‹ ์ตœ๋Œ€ํ™”](./src/_2020_์นด์นด์˜ค_์ธํ„ด์‹ญ/P2) | ๊ตฌํ˜„ |
520521
| 03 | | [๋ณด์„ ์‡ผํ•‘](./src/_2020_์นด์นด์˜ค_์ธํ„ด์‹ญ/P3) | 2-pointer |
521522
| 04 | | [๊ฒฝ์ฃผ๋กœ ๊ฑด์„ค](./src/_2020_์นด์นด์˜ค_์ธํ„ด์‹ญ/P4) | dfs + dp |
522-
| 05 | | [๋™๊ตด ํƒํ—˜](./src/_2020_์นด์นด์˜ค_์ธํ„ด์‹ญ/P5) | graph (cycle check : stack overflow) |
523+
| 05 | โญ๏ธ | [๋™๊ตด ํƒํ—˜](./src/_2020_์นด์นด์˜ค_์ธํ„ด์‹ญ/P5) | graph (cycle check : stack overflow) |

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

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
package Simulation.P21608;
2+
3+
import java.io.*;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
static int N;
9+
static int[][] map, like;
10+
static int[] di = {-1, 0, 1, 0};
11+
static int[] dj = {0, 1, 0, -1};
12+
13+
public static void main(String[] args) throws Exception {
14+
System.setIn(new FileInputStream("src/Simulation/P21608/input.txt"));
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
17+
N = Integer.parseInt(br.readLine());
18+
map = new int[N][N];
19+
like = new int[N*N+1][4];
20+
21+
for (int i = 0; i < N*N; i++) {
22+
StringTokenizer st = new StringTokenizer(br.readLine());
23+
int s = Integer.parseInt(st.nextToken());
24+
for (int j = 0; j < 4; j++) like[s][j] = Integer.parseInt(st.nextToken());
25+
setSeat(s);
26+
}
27+
28+
int score = 0;
29+
for (int i = 0; i < N; i++) {
30+
for (int j = 0; j < N; j++) {
31+
int cnt = 0;
32+
for (int k = 0; k < 4; k++) {
33+
int to_i = i + di[k];
34+
int to_j = j + dj[k];
35+
if (!isValidPath(to_i, to_j)) continue;
36+
for (int l = 0; l < 4; l++) {
37+
if (map[to_i][to_j] == like[map[i][j]][l]) {
38+
cnt ++;
39+
break;
40+
}
41+
}
42+
}
43+
score += getScore(cnt);
44+
}
45+
}
46+
47+
System.out.println(score);
48+
}
49+
50+
static void setSeat(int student) {
51+
PriorityQueue<Seat> pq = new PriorityQueue<>();
52+
for (int i = 0; i < N; i++) {
53+
for (int j = 0; j < N; j++) {
54+
if (map[i][j] > 0) continue;
55+
Seat s = new Seat(i, j);
56+
for (int k = 0; k < 4; k++) {
57+
int to_i = i + di[k];
58+
int to_j = j + dj[k];
59+
if (!isValidPath(to_i, to_j)) continue;
60+
if (map[to_i][to_j] == 0) {
61+
s.emptyCnt ++;
62+
continue;
63+
}
64+
for (int l = 0; l < 4; l++) {
65+
if (map[to_i][to_j] == like[student][l]) {
66+
s.likeCnt ++;
67+
break;
68+
}
69+
}
70+
}
71+
pq.offer(s);
72+
}
73+
}
74+
Seat target = pq.poll();
75+
map[target.i][target.j] = student;
76+
}
77+
78+
static boolean isValidPath(int i, int j) {
79+
return 0 <= i && i < N && 0 <= j && j < N;
80+
}
81+
82+
static int getScore(int cnt) {
83+
if (cnt == 2) return 10;
84+
else if (cnt == 3) return 100;
85+
else if (cnt == 4) return 1000;
86+
else return cnt;
87+
}
88+
89+
static class Seat implements Comparable<Seat> {
90+
int i, j, emptyCnt, likeCnt;
91+
92+
public Seat(int i, int j) {
93+
this.i = i;
94+
this.j = j;
95+
}
96+
97+
@Override
98+
public int compareTo(Seat o) {
99+
if (this.likeCnt == o.likeCnt) {
100+
if (this.emptyCnt == o.emptyCnt) {
101+
if (this.i == o.i) return Integer.compare(this.j, o.j);
102+
return Integer.compare(this.i, o.i);
103+
}
104+
return Integer.compare(o.emptyCnt, this.emptyCnt);
105+
}
106+
return Integer.compare(o.likeCnt, this.likeCnt);
107+
}
108+
}
109+
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
## [baekjoon-21608] ์ƒ์–ด ์ดˆ๋“ฑํ•™๊ต
2+
3+
![image](https://user-images.githubusercontent.com/22045163/116638010-502e3880-a9a0-11eb-8cbe-c1864d635d3d.png)
4+
![image](https://user-images.githubusercontent.com/22045163/116638037-5d4b2780-a9a0-11eb-9fc8-8acdf4b549f6.png)
5+
![image](https://user-images.githubusercontent.com/22045163/116638051-66d48f80-a9a0-11eb-8066-a61bd54f6684.png)
6+
7+
![image](https://user-images.githubusercontent.com/22045163/116638061-70f68e00-a9a0-11eb-90db-e80207d7e18b.png)

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

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

0 commit comments

Comments
ย (0)