Skip to content

Commit fa3cf53

Browse files
authored
[20251119] BOJ / G5 / 상어 초등학교 / 이준희
1 parent 9210986 commit fa3cf53

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
7+
static int N;
8+
static int[][] classroom;
9+
static int[][] likes;
10+
static int[] dr = {-1, 1, 0, 0};
11+
static int[] dc = {0, 0, -1, 1};
12+
13+
public static void main(String[] args) throws Exception {
14+
15+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
16+
N = Integer.parseInt(br.readLine());
17+
18+
classroom = new int[N][N];
19+
likes = new int[N*N + 1][4];
20+
21+
List<Integer> order = new ArrayList<>();
22+
23+
for (int i = 0; i < N * N; i++) {
24+
StringTokenizer st = new StringTokenizer(br.readLine());
25+
int student = Integer.parseInt(st.nextToken());
26+
order.add(student);
27+
28+
for (int j = 0; j < 4; j++) {
29+
likes[student][j] = Integer.parseInt(st.nextToken());
30+
}
31+
}
32+
33+
for (int student : order) {
34+
placeStudent(student);
35+
}
36+
37+
System.out.println(calculateScore());
38+
}
39+
40+
static void placeStudent(int student) {
41+
42+
int bestR = -1, bestC = -1;
43+
int bestLike = -1;
44+
int bestEmpty = -1;
45+
46+
for (int r = 0; r < N; r++) {
47+
for (int c = 0; c < N; c++) {
48+
49+
if (classroom[r][c] != 0) continue;
50+
51+
int likeCnt = 0;
52+
int emptyCnt = 0;
53+
54+
for (int d = 0; d < 4; d++) {
55+
int nr = r + dr[d];
56+
int nc = c + dc[d];
57+
58+
if (nr < 0 || nc < 0 || nr >= N || nc >= N) continue;
59+
60+
if (classroom[nr][nc] == 0) emptyCnt++;
61+
else {
62+
int other = classroom[nr][nc];
63+
if (isLike(student, other)) likeCnt++;
64+
}
65+
}
66+
67+
if (likeCnt > bestLike ||
68+
(likeCnt == bestLike && emptyCnt > bestEmpty) ||
69+
(likeCnt == bestLike && emptyCnt == bestEmpty && r < bestR) ||
70+
(likeCnt == bestLike && emptyCnt == bestEmpty && r == bestR && c < bestC)) {
71+
72+
bestR = r;
73+
bestC = c;
74+
bestLike = likeCnt;
75+
bestEmpty = emptyCnt;
76+
}
77+
}
78+
}
79+
80+
classroom[bestR][bestC] = student;
81+
}
82+
83+
static boolean isLike(int student, int other) {
84+
for (int x : likes[student]) {
85+
if (x == other) return true;
86+
}
87+
return false;
88+
}
89+
90+
static int calculateScore() {
91+
int score = 0;
92+
93+
for (int r = 0; r < N; r++) {
94+
for (int c = 0; c < N; c++) {
95+
96+
int student = classroom[r][c];
97+
int likeCnt = 0;
98+
99+
for (int d = 0; d < 4; d++) {
100+
int nr = r + dr[d];
101+
int nc = c + dc[d];
102+
103+
if (nr < 0 || nc < 0 || nr >= N || nc >= N) continue;
104+
105+
int other = classroom[nr][nc];
106+
if (isLike(student, other)) likeCnt++;
107+
}
108+
109+
if (likeCnt > 0) {
110+
score += (int) Math.pow(10, likeCnt - 1);
111+
}
112+
}
113+
}
114+
115+
return score;
116+
}
117+
}
118+
```

0 commit comments

Comments
 (0)