[JAVA]백준 21608번 상어 초등학교
[JAVA]백준 21608번 상어 초등학교
📌문제 링크
https://www.acmicpc.net/problem/21608
📌문제 설명
N의 크기가 굉장히 작기 때문에 최적화를 신경쓰지 않고 그냥 흐름대로 구현하면 됩니다. 우선순위 큐를 사용해서 문제의 조건을 정렬 하나로 해결했습니다.
📌코드
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import java.io.*;
import java.util.*;
// 학생 정보를 저장하는 클래스
class Student {
int idx, f1, f2, f3, f4; // 학생 번호와 좋아하는 학생 4명
public Student(int idx, int f1, int f2, int f3, int f4) {
this.idx = idx;
this.f1 = f1;
this.f2 = f2;
this.f3 = f3;
this.f4 = f4;
}
}
// 좌표를 저장하는 클래스
class Pair {
int x, y; // 행과 열
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
// 자리 배치 정보를 저장하고 비교하는 클래스
class Comp implements Comparable<Comp> {
int x, y, favoriteCnt, emptyCnt; // 좌표(x, y), 좋아하는 학생 수, 빈 칸 수
public Comp(int x, int y, int favoriteCnt, int emptyCnt) {
this.x = x;
this.y = y;
this.favoriteCnt = favoriteCnt;
this.emptyCnt = emptyCnt;
}
@Override
public int compareTo(Comp o) {
if (this.favoriteCnt != o.favoriteCnt) return o.favoriteCnt - this.favoriteCnt;
if (this.emptyCnt != o.emptyCnt) return o.emptyCnt - this.emptyCnt;
if (this.x != o.x) return this.x - o.x;
return this.y - o.y;
}
}
public class Main {
static BufferedReader br;
static BufferedWriter bw;
static int N;
static List<Student> students = new ArrayList<>();
static int[][] map;
static int[] dx = {0, 0, 1, -1}; // 상하좌우 이동
static int[] dy = {1, -1, 0, 0};
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
input(); // 입력 처리
solve(); // 문제 해결
br.close();
bw.close();
}
// 입력 처리
public static void input() throws Exception {
N = Integer.parseInt(br.readLine()); // 교실 크기 입력
map = new int[N + 1][N + 1]; // 1-based index
for (int i = 0; i < N * N; i++) { // N^2개의 학생 정보 입력
StringTokenizer st = new StringTokenizer(br.readLine());
int idx = Integer.parseInt(st.nextToken());
int f1 = Integer.parseInt(st.nextToken());
int f2 = Integer.parseInt(st.nextToken());
int f3 = Integer.parseInt(st.nextToken());
int f4 = Integer.parseInt(st.nextToken());
students.add(new Student(idx, f1, f2, f3, f4));
}
}
// 학생 자리 배치
public static void assignSeat(Student student) {
PriorityQueue<Comp> pq = new PriorityQueue<>();
for (int x = 1; x <= N; x++) {
for (int y = 1; y <= N; y++) {
if (map[x][y] != 0) continue; // 이미 배정된 자리라면 건너뜀
int favoriteCnt = 0, emptyCnt = 0;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (nx < 1 || ny < 1 || nx > N || ny > N) continue;
if (map[nx][ny] == student.f1 || map[nx][ny] == student.f2 ||
map[nx][ny] == student.f3 || map[nx][ny] == student.f4) {
favoriteCnt++;
}
if (map[nx][ny] == 0) {
emptyCnt++;
}
}
pq.add(new Comp(x, y, favoriteCnt, emptyCnt));
}
}
if (!pq.isEmpty()) {
Comp bestSeat = pq.poll();
map[bestSeat.x][bestSeat.y] = student.idx;
}
}
// 학생의 만족도 계산
public static int calculateSatisfaction() {
int totalSatisfaction = 0;
for (Student student : students) {
Pair loc = findStudentLocation(student);
int x = loc.x, y = loc.y;
int count = 0;
for (int d = 0; d < 4; d++) {
int nx = x + dx[d], ny = y + dy[d];
if (nx < 1 || ny < 1 || nx > N || ny > N) continue;
if (map[nx][ny] == student.f1 || map[nx][ny] == student.f2 ||
map[nx][ny] == student.f3 || map[nx][ny] == student.f4) {
count++;
}
}
if (count == 1) totalSatisfaction += 1;
else if (count == 2) totalSatisfaction += 10;
else if (count == 3) totalSatisfaction += 100;
else if (count == 4) totalSatisfaction += 1000;
}
return totalSatisfaction;
}
// 학생의 위치 찾기
public static Pair findStudentLocation(Student student) {
for (int x = 1; x <= N; x++) {
for (int y = 1; y <= N; y++) {
if (map[x][y] == student.idx) {
return new Pair(x, y);
}
}
}
return null; // 이론상 실행되지 않음
}
// 문제 해결
public static void solve() throws Exception {
for (Student student : students) {
assignSeat(student);
}
int satisfaction = calculateSatisfaction();
bw.write(satisfaction + "\n");
bw.flush();
}
}
This post is licensed under CC BY 4.0 by the author.