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

#include<bits/stdc++.h>
#define rep(i,k,n) for(ll i= (ll) k;i< (ll) n;i++)
#define all(v) (v).begin(), (v).end()
#define SZ(v) (int)((v).size())
#define pb push_back
#define ft first
#define sd second
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const long long INF = 4e18L + 1;
const int IINF = 2e9 + 1;

using namespace std;

template<class TH> void _dbg(const char *sdbg, TH h){ cerr<<sdbg<<'='<<h<<endl; }
template<class TH, class... TA> void _dbg(const char *sdbg, TH h, TA... a) {
  while(*sdbg!=',')cerr<<*sdbg++;
  cerr<<'='<<h<<','; _dbg(sdbg+1, a...);
}

#ifdef LOCAL
#define DBG(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
#else
#define DBG(...)
#define cerr if(0)cout
#endif

char tab[1005][1005];
int TT[1005][1005];
int ile_osad;
int xo, yo;
int aktt;


bool ok()
{
	aktt++;
	int ile_mam = 1;
	TT[xo][yo] = aktt;
	queue < pair < int, int > > Q;
	Q.push({xo, yo});
	while(!Q.empty())
	{
		auto para = Q.front();
		int x = para.ft, y = para.sd;
		Q.pop();
		
		vector < pair < int, int > > kand = { {x+1, y}, {x-1, y}, {x, y-1}, {x, y+1}};
		for(auto el : kand)
		{
			x = el.ft, y = el.sd;
			if (TT[x][y] == aktt)
				continue;
			TT[x][y] = aktt;
			if (tab[x][y] == 'K' || tab[x][y] == '.')
			{
				Q.push({x, y});
				if (tab[x][y] == 'K')
					ile_mam++;
			}
		}
	}
	return ile_mam == ile_osad;
}

void NowaWyspa(int n, int m, char **Board)
{
	for(int i=1; i<=n; i++)
		for(int j=1; j<=m; j++)
		{
			tab[i][j] = Board[i-1][j-1];
			if (tab[i][j] == 'K')
			{
				ile_osad++;
				xo = i, yo = j;
			}
		}
}

int NowaWarownia(int r, int c)
{
	char pop = tab[r][c];
	tab[r][c] = 'W';
	if (!ok())
	{
		tab[r][c] = pop;
		return 0;
	}
	return 1;
	
}
void PrzeniesOsade(int r1, int c1, int r2, int c2)
{
	xo = r2, yo = c2;
	swap(tab[r1][c1], tab[r2][c2]);
}