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
#include <bits/stdc++.h>
#include "osalib.h"

using namespace std;

const int N = 1007;
const int DX[] = {-1, 0, 0, 1};
const int DY[] = {0, -1, 1, 0};

int n, m;
char s[N][N];
int vis[N][N];

int ile = 0;
int cnt = 0;
int licz = 0;
int curx, cury;

void NowaWyspa(int nn, int mm, char **board){
	n = nn; m = mm;
	for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j){
			s[i][j] = board[i - 1][j - 1];
			if(s[i][j] == 'K'){
				curx = i, cury = j;
				++ile;
			}
		}
}

void PrzeniesOsade(int r1, int c1, int r2, int c2){
	s[r1][c1] = '.'; s[r2][c2] = 'K';
	curx = r2, cury = c2;
}

inline bool ok(int x, int y){
	return 1 <= x && x <= n && 1 <= y && y <= m && vis[x][y] < cnt && s[x][y] != 'W';
}

void dfs(int x, int y){
	vis[x][y] = cnt;
	licz += s[x][y] == 'K';
	
	for(int i = 0; i < 4; ++i)
		if(ok(x + DX[i], y + DY[i]))
			dfs(x + DX[i], y + DY[i]);
}

int NowaWarownia(int r, int c){
	s[r][c] = 'W'; 
	licz = 0; ++cnt;
	dfs(curx, cury);

	if(licz != ile){
		s[r][c] = '.';
		return false;
	}
	
	return true;
}
/*
int main(){
	int nn, mm;
	scanf("%d %d", &nn, &mm);
	char *board[nn];
	for(int i = 0; i < nn; ++i){
		board[i] = (char *)malloc(mm);
		for(int j = 0; j < mm; ++j)
			scanf(" %c", &board[i][j]);
	}
	
	NowaWyspa(nn, mm, board);
	int q; scanf("%d", &q);
	for(int i = 1; i <= q; ++i){
		int t; scanf("%d", &t);
		if(t == 1){
			int a, b, c, d;
			scanf("%d %d %d %d", &a, &b, &c, &d);
			PrzeniesOsade(a, b, c, d);
		}
		else{
			int x, y;
			scanf("%d %d", &x, &y);
			if(NowaWarownia(x, y))
				puts("TAK");
			else
				puts("NIE");
		}
	}

	return 0;
}*/