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

using namespace std;

char** wyspa;
int w, h;

bool canGo(int x, int y);

void NowaWyspa(int n, int m, char** board) {
    wyspa = board;
    w = m;
    h = n;
}

int NowaWarownia(int r, int c) {
    wyspa[r-1][c-1] = 'W';
//    bool visited[h][w] = {};
    bool** visited = new bool*[h];
    for (int i = 0; i < h; i++) {
        visited[i] = new bool[w];
    }
    queue<int> toVisitX;
    queue<int> toVisitY;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (wyspa[i][j] == 'K') {
                toVisitY.push(i);
                toVisitX.push(j);
                break;
            }
        }
        if (!toVisitX.empty())
            break;
    }
    while (!toVisitX.empty()) {
        visited[toVisitY.front()][toVisitX.front()] = true;
        if (canGo(toVisitY.front()-1, toVisitX.front()) && !visited[toVisitY.front() - 1][toVisitX.front()]) {
            toVisitY.push(toVisitY.front()-1);
            toVisitX.push(toVisitX.front());
        }
        if (canGo(toVisitY.front()+1, toVisitX.front()) && !visited[toVisitY.front() + 1][toVisitX.front()]) {
            toVisitY.push(toVisitY.front()+1);
            toVisitX.push(toVisitX.front());
        }
        if (canGo(toVisitY.front(), toVisitX.front()-1) && !visited[toVisitY.front()][toVisitX.front() - 1]) {
            toVisitY.push(toVisitY.front());
            toVisitX.push(toVisitX.front()-1);
        }

        if (canGo(toVisitY.front(), toVisitX.front()+1) && !visited[toVisitY.front()][toVisitX.front() + 1]) {
            toVisitY.push(toVisitY.front());
            toVisitX.push(toVisitX.front()+1);
        }

        toVisitX.pop();
        toVisitY.pop();
    }

    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (!visited[i][j] && wyspa[i][j] == 'K') {
                wyspa[r-1][c-1] = '.';
                return 0;
            }
        }
    }

    return 1;
}

void PrzeniesOsade(int r1, int c1, int r2, int c2) {
    swap(wyspa[r1-1][c1-1], wyspa[r2-1][c2-1]);
}

bool canGo(int y, int x) {
    return x >= 0 && x < w && y >= 0 && y < h && wyspa[y][x] != 'W';
}