-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path21608.cpp
91 lines (82 loc) · 1.54 KB
/
21608.cpp
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
#include <iostream>
using namespace std;
typedef long long ll;
const int MAX = 900;
int arr[MAX][MAX];
int likes[MAX][4];
int N;
int xxxx[]={-1,0,1,0};
int yyyy[]={0,1,0,-1};
bool ob(int x,int y) {
return x<1 || y < 1 || x > N || y > N;
}
int sum(int x, int y) {
int a=0, n = arr[x][y];
for (int i = 0; i < 4; i++) {
int nx=x+xxxx[i];
int ny=y+yyyy[i];
if(ob(nx,ny)) continue;
for (int j = 0; j < 4; j++) {
if (arr[nx][ny] == likes[n][j]) {
a ? a*=10 : a=1;
break;
}
}
}
return a;
}
pair<int,int> point(int x, int y, int n) {
int a=0,b=0;
for (int i = 0; i < 4; i++) {
int nx=x+xxxx[i];
int ny=y+yyyy[i];
if(ob(nx,ny)) continue;
if (arr[nx][ny] == 0) {
b++;
continue;
}
for (int j = 0; j < 4; j++) {
if (arr[nx][ny] == likes[n][j]) {
a++;
break;
}
}
}
return {a,b};
}
void solve(){
cin >> N;
for (int i = 1; i<=N*N;i++) {
int a; cin >> a;
for(int j = 0; j < 4; j++) {
int b; cin >> b;
likes[a][j] = b;
}
pair<int,int> p = {-1,0};
int c,d;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
if (arr[i][j]) continue;
if (p < point(i,j,a)) {
p = point(i,j,a);
c = i;
d = j;
}
}
}
arr[c][d]=a;
}
ll result = 0;
for (int i = 1; i <= N; i++) {
for (int j = 1; j <= N; j++) {
result += sum(i,j);
}
}
cout << result;
}
int main()
{
cin.tie(0)->sync_with_stdio(0);
solve();
return 0;
}