1
+ import java .io .*;
2
+ import java .util .*;
3
+
4
+ public class SB_놀이기구_탑승 {
5
+ static int N ;
6
+ static int [][] board ;
7
+ static int [][] emptyCnt ;
8
+ static List <List <Integer >> friends = new ArrayList <>();
9
+ static PriorityQueue <Node > pq = new PriorityQueue <Node >();
10
+ static int [] dx = {-1 , 1 , 0 , 0 };
11
+ static int [] dy = {0 , 0 , -1 , 1 };
12
+
13
+ private static int checkNxt (int x , int y , int idx ) {
14
+ int cnt = 0 ;
15
+ for (int i = 0 ; i < 4 ; i ++) {
16
+ int nx = x + dx [i ];
17
+ int ny = y + dy [i ];
18
+ if (!isValid (nx , ny )) continue ;
19
+ if (friends .get (idx ).contains (board [nx ][ny ])) cnt ++;
20
+ }
21
+ return cnt ;
22
+ }
23
+ private static void findSit (int idx ) {
24
+ for (int i = 0 ; i < N ; i ++) {
25
+ for (int j = 0 ; j < N ; j ++) {
26
+ if (board [i ][j ]==0 ) {
27
+ int like = checkNxt (i , j , idx );
28
+ pq .offer (new Node (i , j , like , emptyCnt [i ][j ]));
29
+ }
30
+ }
31
+ }
32
+ fixSit (idx );
33
+ }
34
+
35
+ private static void fixSit (int idx ) {
36
+ if (!pq .isEmpty ()) {
37
+ Node cur = pq .poll ();
38
+ board [cur .r ][cur .c ] = idx ;
39
+
40
+ // 빈자리 수 업데이트
41
+ emptyCnt [cur .r ][cur .c ]--;
42
+ for (int i = 0 ; i < 4 ; i ++) {
43
+ int nx = cur .r + dx [i ];
44
+ int ny = cur .c + dy [i ];
45
+ if (isValid (nx , ny )) {
46
+ emptyCnt [nx ][ny ]--;
47
+ }
48
+ }
49
+ }
50
+ pq .clear ();
51
+ }
52
+
53
+ private static boolean isValid (int x , int y ) {
54
+ return 0 <=x && x <N && 0 <=y && y <N ;
55
+ }
56
+ public static void main (String [] args ) throws IOException {
57
+ BufferedReader br = new BufferedReader (new InputStreamReader (System .in ));
58
+ StringTokenizer st ;
59
+ N = Integer .parseInt (br .readLine ());
60
+
61
+ board = new int [N ][N ];
62
+ emptyCnt = new int [N ][N ];
63
+
64
+ // emptyCnt 초기화
65
+ for (int i = 0 ; i < N ; i ++) {
66
+ for (int j = 0 ; j < N ; j ++) {
67
+ emptyCnt [i ][j ] = 4 ;
68
+ if (i ==0 || i ==N -1 ) emptyCnt [i ][j ]--;
69
+ if (j ==0 || j ==N -1 ) emptyCnt [i ][j ]--;
70
+ if (emptyCnt [i ][j ]==4 ) pq .offer (new Node (i , j , 0 , 4 ));
71
+ }
72
+ }
73
+ // friends 리스트 초기화
74
+ for (int i = 0 ; i <= N * N ; i ++) {
75
+ friends .add (new ArrayList <>());
76
+ }
77
+
78
+ for (int i = 0 ; i < N * N ; i ++) {
79
+ st = new StringTokenizer (br .readLine ());
80
+ int me = Integer .parseInt (st .nextToken ());
81
+ for (int j = 0 ; j < 4 ; j ++) {
82
+ int f = Integer .parseInt (st .nextToken ());
83
+ friends .get (me ).add (f );
84
+ }
85
+ if (i ==0 ) fixSit (me ); // 처음사람은 바로 자리 고정
86
+ else findSit (me );
87
+ }
88
+
89
+ // 점수 카운트
90
+ int [] score = {0 , 1 , 10 , 100 , 1000 };
91
+ int ans = 0 ;
92
+ for (int i = 0 ; i < N ; i ++) {
93
+ for (int j = 0 ; j < N ; j ++) {
94
+ int like = checkNxt (i , j , board [i ][j ]);
95
+ ans += score [like ];
96
+ }
97
+ }
98
+ System .out .println (ans );
99
+ }
100
+ private static class Node implements Comparable <Node >{
101
+ int r , c , like , empty ;
102
+
103
+ Node (int r , int c , int like , int empty ) {
104
+ this .r = r ;
105
+ this .c = c ;
106
+ this .like = like ;
107
+ this .empty = empty ;
108
+ }
109
+
110
+ @ Override
111
+ public int compareTo (Node o ) {
112
+ int cmp = Integer .compare (o .like , this .like );
113
+ if (cmp == 0 ) {
114
+ int cmp2 = Integer .compare (o .empty , this .empty );
115
+ if (cmp2 == 0 ) {
116
+ int cmp3 = Integer .compare (this .r , o .r );
117
+ if (cmp3 ==0 ){
118
+ return Integer .compare (this .c , o .c );
119
+ } return cmp3 ;
120
+ }return cmp2 ;
121
+ }
122
+ return cmp ;
123
+ }
124
+ }
125
+ }
0 commit comments