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

#define FOR(i,b,e) for(int i=(b); i <= (e); ++i)
#define FORD(i,b,e) for(int i=(b); i >= (e); --i)
#define SIZE(c) (int) (c).size()


using namespace std;

typedef vector <bool> VB;
typedef vector <VB> VVB;

/*************************************************************************/

namespace Solution {
const int MAXN = 1002;

int n, m, marked;
char board[MAXN][MAXN];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

bool valid(int x, int y) {
    return x >= 0 && x < n && y >= 0 && y < m;
}

int dfsCnt(int x, int y, VVB &vis) {
    int res = (board[x][y] == 'K');
    vis[x][y] = true;
    
    FOR(dir,0,3) {
        int nx = x + dx[dir];
        int ny = y + dy[dir];
        
        if (valid(nx, ny) && !vis[nx][ny] && board[nx][ny] != 'W') {
            res += dfsCnt(nx, ny, vis);
        }
    }
    
    return res;
}

bool checkBoard() {
    VVB vis(n, VB(m, false));
    
    FOR(x,0,n-1) FOR(y,0,m-1) if (board[x][y] == 'K') {
        return dfsCnt(x, y, vis) == marked;
    }
    
    return true;
}
};

void NowaWyspa(int n, int m, char **board) {
    Solution::n = n;
    Solution::m = m;
    Solution::marked = 0;
    
    FOR(i,0,n-1) FOR(j,0,m-1) {
        Solution::board[i][j] = board[i][j];
        Solution::marked += (board[i][j] == 'K');
    }
}

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

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

/*************************************************************************/