-
Notifications
You must be signed in to change notification settings - Fork 0
/
상어 초등학교.cc
97 lines (87 loc) · 2.19 KB
/
상어 초등학교.cc
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
#include <iostream>
#include <algorithm>
#include <unordered_set>
#include <cmath>
using namespace std;
int n;
vector<vector<int>> seats;
vector<unordered_set<int>> friends;
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
int findBest(int x, int y, int s) {
int nx, ny, score = 0;
for (int i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
continue;
}
if (seats[nx][ny] == 0) {
score += 1;
}
else {
if (friends[s-1].find(seats[nx][ny]) != friends[s-1].end()) {
score += 5;
}
}
}
return score;
}
void getSeat(int s) {
int x,y;
int bestScore = -1, score = -1;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (seats[i][j] == 0) {
score = findBest(i,j,s);
if (score > bestScore) {
x = i;
y = j;
bestScore = score;
}
}
}
}
seats[x][y] = s;
return;
}
int getSatisfyScore(int x, int y) {
int nx, ny, num = 0, s = seats[x][y];
for (int i = 0; i < 4; i++) {
nx = x + dx[i];
ny = y + dy[i];
if (nx < 0 || nx >= n || ny < 0 || ny >= n) {
continue;
}
if (friends[s-1].find(seats[nx][ny]) != friends[s-1].end()) {
num++;
}
}
return pow(10, num - 1);
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("input.txt", "r", stdin);
cin >> n;
seats.assign(n,vector<int>(n,0));
friends.assign(n * n, unordered_set<int>());
int s;
int f[4];
for(int i = 0; i < n * n; i++) {
cin >> s >> f[0] >> f[1] >> f[2] >> f[3];
for (int i = 0; i < 4; i++) {
friends[s-1].insert(f[i]);
}
getSeat(s);
}
int result = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
result += getSatisfyScore(i,j);
}
}
cout << result;
return 0;
}