|
| 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 | +} |
0 commit comments