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

char s[1007][1007];
int odw[1007][1007];
int n, m;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

vector <pair <int, int>> osady;

void NowaWyspa(int ng, int mg, char **board) {
    n = ng;
    m = mg;
    for(int i = 0; i < n; ++i) for(int j = 0; j < m; ++j) s[i + 1][j + 1] = board[i][j];
    
    for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) if(s[i][j] == 'K') osady.push_back({i, j});
}

void dfs(int i, int j) {
    odw[i][j] = 1;
    bool ok[4] = {j != 1, i != n, j != m, i != 1};
    for(int k = 0; k < 4; ++k) {
        if(ok[k] && !odw[i + dy[k]][j + dx[k]] && s[i + dy[k]][j + dx[k]] != 'W') dfs(i + dy[k], j + dx[k]);
    }
}

int NowaWarownia(int xi, int xj) {
    s[xi][xj] = 'W';
    if(osady.empty()) return 1;    
    
    for(int i = 1; i <= n; ++i) for(int j = 1; j <= m; ++j) odw[i][j] = 0;
   
    dfs(osady.back().first, osady.back().second);
    
    int ok = 1;
    for(auto u : osady) if(!odw[u.first][u.second]) ok = 0;

    if(ok) return 1;
    else {
        s[xi][xj] = '.';
        return 0;
    }
}

void PrzeniesOsade(int r1, int c1, int r2, int c2) {
    for(auto &u : osady) {
        if(u.first == r1 && u.second == c1) {
            u.first = r2;
            u.second = c2;
        }
    }
}