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
#include <cstdio>
#include <queue>
#include <utility>
#include "osalib.h"
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define MP make_pair
#define FT first
#define SD second

typedef pair<int,int> PII;

const int DX[4] = {0, 1, 0, -1}, DY[4] = {1, 0, -1, 0};
char **board;
int n, m;
int kx = -1, ky = -1;
int ks;
int been[1000][1000];
int stage;

bool check() {
	if (kx < 0) return 1;
	queue<PII> q;
	been[kx][ky] = ++stage;
	q.push(MP(kx, ky));
	int c = 0;
	while (!q.empty()) {
		PII p = q.front();
		q.pop();
		if (board[p.FT][p.SD] == 'K') ++c;
		if (c == ks) return 1;
		REP(k,4) {
			int x = p.FT + DX[k], y = p.SD + DY[k];
			if (x < 0 || x >= n || y < 0 || y >= m || board[x][y] == 'W' || been[x][y] == stage) continue;
			been[x][y] = stage;
			q.push(MP(x, y));
		}
	}
	return 0;
}

#ifdef __cplusplus
extern "C" {
#endif

void NowaWyspa(int _n, int _m, char **Board) {
	board = Board;
	n = _n;
	m = _m;
	REP(i,n) REP(j,m) if (board[i][j] == 'K') {
		if (kx < 0) {
			kx = i;
			ky = j;
		}
		++ks;
	}
}

int NowaWarownia(int r, int c) {
	board[r - 1][c - 1] = 'W';
	if (check()) return 1;
	board[r - 1][c - 1] = '.';
	return 0;
}

void PrzeniesOsade(int r1, int c1, int r2, int c2) {
	board[r1 - 1][c1 - 1] = '.';
	board[r2 - 1][c2 - 1] = 'K';
}

#ifdef __cplusplus
}
#endif