-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20058.c
170 lines (140 loc) · 2.16 KB
/
20058.c
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
165
166
167
168
#include <stdio.h>
#define MAX 64
#define NRANGE 6
//#define max(a, b) (((a) > (b)) ? (a) : (b))
//doesn't work in this case due to visited
int max(int x, int y)
{
return x > y ? x : y;
}
int N, Q;
int A[MAX][MAX], temp[MAX][MAX];
int L[NRANGE];
int visited[MAX][MAX];
const int dx[] = {0, 0, -1, 1};
const int dy[] = {-1, 1, 0, 0};
int inRange(int x, int y)
{
return x >= 0 && y >= 0 && x < N && y < N;
}
int dfs(int x, int y)
{
int i;
int ret = 1;
visited[x][y] = 1;
for(i=0; i<4; i++)
{
int ny = y + dy[i];
int nx = x + dx[i];
if(inRange(nx, ny) && !visited[nx][ny] && A[nx][ny] > 0)
ret += dfs(nx, ny);
}
return ret;
}
int getBiggest()
{
int ret = 0;
int i, j;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
if(A[i][j] > 0 && !visited[i][j])
ret = max(ret, dfs(i, j));
return ret;
}
int getSum()
{
int i, j;
int ret = 0;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
ret += A[i][j];
return ret;
}
void melt(void)
{
int i, j, k;
int cnt;
for(i=0; i<N; i++)
for(j=0; j<N; j++)
{
cnt = 0;
for(k=0; k<4; k++)
{
int nx = i + dx[k];
int ny = j + dy[k];
if(!inRange(nx, ny))
continue;
else if(temp[nx][ny] > 0)
cnt++;
}
if(temp[i][j])
A[i][j] = cnt<3 ? (temp[i][j] - 1) : temp[i][j];
else
A[i][j] = 0;
}
}
void rotate(int x, int y, int L)
{
int i, j;
int ys=0, xa=0;
x *= L, y *= L;
for(i=0; i<L; i++)
{
for(j=0; j<L; j++)
{
temp[x+(xa++)][y+L-1-ys] =A[x+i][y+j];
}
ys++;
xa = 0;
}
/*
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%d ", temp[i][j]);
printf("\n");
}
*/
return;
}
void solve(int L)
{
int i, j;
L = (1<<L);
for(i = 0; i<N/L; i++)
for(j=0; j<N/L; j++)
rotate(i, j, L); // in temp
/*
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%d ", temp[i][j]);
printf("\n");
}
*/
melt();
}
int main(void)
{
int i, j;
scanf("%d %d", &N, &Q);
N = (1<<N);
for(i=0; i<N; i++)
for(j=0; j<N; j++)
scanf("%d", &A[i][j]);
for(i=0; i<Q; i++)
{
scanf("%d", &L[i]);
solve(L[i]);
}
/*
for(i=0; i<N; i++)
{
for(j=0; j<N; j++)
printf("%d ", A[i][j]);
printf("\n");
}
*/
printf("%d %d", getSum(), getBiggest());
return 0;
}