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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include <algorithm>
#include <string>
#include <vector>
#include <deque>
#include <unordered_map>
#include <climits>
#include <sstream>
#include <set>
#define LL long long

using namespace std;

#define MAX_N 3001

#define DBG(X)
int rozmiar_planszy;
int liczba_graczy;

#define MODULO 1000000007

const int delta_xy[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int delta_xy_cnt = 4;

inline bool is_inside(int x, int low, int high) {
	return (low <= x && x < high);
}

bool sasiaduje_z_(char** plansza, char c, int x, int y) {
	for (int i = 0; i < delta_xy_cnt; i++)
	{
		int nx = x + delta_xy[i][0];
		int ny = y + delta_xy[i][1];
		if (is_inside(nx, 0, rozmiar_planszy) && is_inside(ny, 0, rozmiar_planszy)) {
			if (plansza[nx][ny] == c) {
				return true;
			}
		}
	}
	return false;
}

int ilu_sasiaduje_z_(char** plansza, char with_whom, int x, int y) {
	int res = 0;
	for (int i = 0; i < delta_xy_cnt; i++)
	{
		int nx = x + delta_xy[i][0];
		int ny = y + delta_xy[i][1];
		if (is_inside(nx, 0, rozmiar_planszy) && is_inside(ny, 0, rozmiar_planszy)) {
			if (plansza[nx][ny] == with_whom) {
				res++;
			}
		}
	}
	return res;
}

int policz_sasiadujacych(char** plansza, int x, int y, char output[5]) {
	int res = 0;
	for (int i = 0; i < 5; i++) {
		output[i] = 0;
	}
	for (int i = 0; i < delta_xy_cnt; i++)
	{
		int nx = x + delta_xy[i][0];
		int ny = y + delta_xy[i][1];
		if (is_inside(nx, 0, rozmiar_planszy) && is_inside(ny, 0, rozmiar_planszy)) {
			char c = plansza[nx][ny];
			if (c >='1' && c <='4') {
				output[c - '0']++;
			}
		}
	}
	return res;
}

void print_plansza(char** plansza, int rozmiar_planszy) {
	printf("Plansza:\n");
	for (int i = 0; i < rozmiar_planszy; i++) {
		printf("%s\n", plansza[i]);
	}
}

LL policz_pattern(char** plansza, char* pattern, int x, int y)
{
	//printf("(%d, %d, %s) ", x, y, pattern);
	if (!is_inside(x, 0, rozmiar_planszy)) {
		return 0;
	}
	if (!is_inside(y, 0, rozmiar_planszy)) {
		return 0;
	}
	if (plansza[x][y] != *pattern) {
		return 0;
	}
	if (*(pattern+1) == 0) {
		// we've reached end of pattern, success
		return 1;
	}
	// mark as visited
	plansza[x][y] = 7;
	LL res = 0;
	for (int k = 0; k < delta_xy_cnt; k++)
	{
		res += policz_pattern(plansza, pattern + 1, x + delta_xy[k][0], y + delta_xy[k][1]);
	}
	// unmark as visited
	plansza[x][y] = *pattern;
	return res;
}

struct PlanszaStats {
	// ile znakow x na planszy, np. counters[1] mowi o tym ile jest jedynek
	LL counters[5];

	// np. ile jedynek z 1 dwojka, ile jedynek z dwiema dwojkami, ile jedynek z 3 ma dwojkami, ile jedynek z 4-ma dwojkami
	// np. ile dwojek z 1 jedynka, ile 2 z 2 jedynkami, ile 2 z 3 jedynkami, ile 2 z 4 jedynkami

	LL cntXzNofY[5][5][5];


	PlanszaStats() {
		for (int i = 0; i < 5; i++) {
			counters[i] = 0;
		}
		for (int i = 0; i < 5; i++) {
			for (int j = 0; j < 5; j++) {
				for (int k = 0; k < 5; k++) {
					cntXzNofY[i][j][k] = 0;
				}
			}
		}
	}
};

bool preprocess(char** plansza, int rozmiar_planszy, PlanszaStats &plansza_stats) {
	char** plansza_copy = new char*[rozmiar_planszy + 2];
	for (int i = 0; i < rozmiar_planszy; i++) {
		plansza_copy[i] = new char[rozmiar_planszy + 2];
		plansza_copy[i][rozmiar_planszy] = 0;
	}

	int char_list[] = { '#', '1', '2', '3', '4' };
	char** ptrs[] = { plansza, plansza_copy };
	int current_ptr = 0;
	for (int ci=1; ci < 6; ci++) {
		char previous_c = char_list[ci-1];
		char next_c = char_list[ci];
		char** current_plansza = ptrs[current_ptr % 2];
		char** next_plansza = ptrs[(current_ptr + 1) % 2];
		for (int i = 0; i < rozmiar_planszy; i++) {
			for (int j = 0; j < rozmiar_planszy; j++) {
				next_plansza[i][j] = current_plansza[i][j];
				if (current_plansza[i][j] == '.') {
					if (sasiaduje_z_(current_plansza, previous_c, i, j)) {
						next_plansza[i][j] = next_c;
					}
				}
			}
		}
		current_ptr++;
	}
	DBG(print_plansza(plansza, rozmiar_planszy));
	char output[5];
	for (int i = 0; i < rozmiar_planszy; i++) {
		for (int j = 0; j < rozmiar_planszy; j++) {
			char c = plansza[i][j];
			if (c >= '1' && c <= '4') {
				plansza_stats.counters[c - '0']++;
				policz_sasiadujacych(plansza, i, j, output);
				for (int cs = 1; cs <= 4; cs++) {
					plansza_stats.cntXzNofY[c - '0'][output[cs]][cs]++;
				}
			}
		}
	}
	for (int i = 0; i < rozmiar_planszy; i++) {
		delete[] plansza_copy[i];
	}
	delete[] plansza_copy;
	return 0;
}

LL solve_k_1(PlanszaStats& ps)
{
	return ps.counters[1];
}

LL solve_k_2(PlanszaStats& ps) {
	LL res = 0;
	res += (ps.counters[1] * (ps.counters[1] - 1)) / 2;
	res %= MODULO;
	for (int i = 1; i <= 4; i++) {
		res += ps.cntXzNofY[2][i][1] * i; // liczba dwojek z 'i' jedynkami jako sasiadami
		res %= MODULO;
	}
	return res;
}

LL solve_k_3(PlanszaStats& ps, char** plansza, int rozmiar_planszy)
{
	LL res = 0;
	LL a, b, c;
	a = ps.counters[1];
	b = a - 1;
	c = a - 2;
	if (a % 3 == 0)
	{
		a /= 3;
	}
	else if (b % 3 == 0) {
		b /= 3;
	}
	else if (c % 3 == 0) {
		c /= 3;
	}
	if (a % 2 == 0) {
		a /= 2;
	}
	else if (b % 2 == 0) {
		b /= 2;
	}
	else if (c % 2 == 0) {
		c /= 2;
	}
	LL r111 = a * b;
	r111 %= MODULO;
	r111 *= c;

	res += r111;
	LL r112 = ps.cntXzNofY[2][1][1] * (ps.counters[1] - 1);
	r112 += ps.cntXzNofY[2][2][1] * 2 * (ps.counters[1] - 1) - ps.cntXzNofY[2][2][1];
	r112 += ps.cntXzNofY[2][3][1] * 3 * (ps.counters[1] - 1) - ps.cntXzNofY[2][3][1] * 3; // inaczej: n2z3 *(n1-1) + n2z3 * (n1-2) + n2z3 * (n1-3) => n3z3*3*n1 - 6*n2z3
	r112 += ps.cntXzNofY[2][4][1] * 4 * (ps.counters[1] - 1) - ps.cntXzNofY[2][4][1] * 6;

	res += r112;
	res %= MODULO;

	char pattern221[5] = "221";
	char pattern212[5] = "212";
	char pattern321[5] = "321";
	LL cnt_pattern221 = 0;
	LL cnt_pattern212 = 0;
	LL cnt_pattern321 = 0;

	for (int i = 0; i < rozmiar_planszy; i++) {
		for (int j = 0; j < rozmiar_planszy; j++) {
			cnt_pattern221 += policz_pattern(plansza, pattern221, i, j);
			cnt_pattern212 += policz_pattern(plansza, pattern212, i, j);
			cnt_pattern321 += policz_pattern(plansza, pattern321, i, j);
		}
	}
	res += cnt_pattern221;
	res += cnt_pattern212 / 2;
	res += cnt_pattern321;
	res %= MODULO;

	return res;
}

LL calc4ones_modulo(LL cnt)
{
	LL tmp[4];
	tmp[0] = cnt;
	for (int i = 1; i < 4; i++) {
		tmp[i] = tmp[i - 1] - 1;
	}
	LL divisors[] = { 2,2,2,3 };
	for (int i = 0; i < 4; i++) {
		LL d = divisors[i];
		for (int j = 0; j < 4; j++) {
			if (tmp[j] % d == 0) {
				tmp[j] /= d;
				break;
			}
		}
	}
	LL res = tmp[0] * tmp[1];
	res %= MODULO;
	res *= tmp[2];
	res %= MODULO;
	res *= tmp[3];
	res %= MODULO;

	return res;
}

LL solve_k_4(PlanszaStats& ps, char** plansza, int rozmiar_planszy)
{
	LL res = calc4ones_modulo(ps.counters[1]);
	// TODO:
	return res;
}

LL solve(char** plansza, int rozmiar_planszy, int liczba_graczy)
{
	PlanszaStats plansza_stats;
	preprocess(plansza, rozmiar_planszy, plansza_stats);
	LL res = 0;

	if (liczba_graczy == 1)
	{
		return solve_k_1(plansza_stats);
	}
	if (liczba_graczy == 2)
	{
		return solve_k_2(plansza_stats);
	}
	if (liczba_graczy == 3)
	{
		return solve_k_3(plansza_stats, plansza, rozmiar_planszy);
	}
	if (liczba_graczy == 4)
	{
		return solve_k_4(plansza_stats, plansza, rozmiar_planszy);
	}
}

int main() {
	scanf("%d%d", &rozmiar_planszy, &liczba_graczy);
	const int n = rozmiar_planszy;
	char** plansza = new char*[rozmiar_planszy+2];
	for (int i = 0; i < rozmiar_planszy; i++) {
		plansza[i] = new char[rozmiar_planszy + 2];
		scanf("%s", plansza[i]);
	}
	LL res = solve(plansza, rozmiar_planszy, liczba_graczy);
	printf("%lld\n", res);
	return 0;
}