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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#include <bits/stdc++.h>
// #pragma GCC optimize ("O3")
// #pragma GCC target ("sse4")
using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,a,b) for (int i=(a); i<(b); ++i)
#define FORD(i,a,b) for (int i=(a)-1; i>=(b); --i)

#define pb push_back
#define mp make_pair
#define st first
#define nd second

#define dprintf(...) printf(__VA_ARGS__)

#include "osalib.h"

int N, M;
char B[2000][2000];
bool safe[2000][2000];

int _component[2000][2000];
int _find[2000000];

int& find(int c) {
  if (_find[c] != c) _find[c] = find(_find[c]);
  return _find[_find[c]];
}

int& component(int r, int c) {
  int& res = find(_component[r][c]);
  return res;
}

int OSADY;

int CNUM;
void find_components(int r, int c) {
  if (r < 0 || r >= N || c < 0 || c >= M || _component[r][c] > -1 || B[r][c] != 'W') return;

  _component[r][c] = CNUM;
  FOR(i,-1,2)FOR(j,-1,2) find_components(r+i, c+j);
}

void NowaWyspa(int n, int m, char **board) {
  OSADY = 0;
  N = n, M = m;
  REP(i,N+2)REP(j,M+2) B[i][j] = 'W';
  REP(i,N)REP(j,M) {
    B[i+1][j+1] = board[i][j];
    OSADY += B[i+1][j+1] == 'K';
  }

  N += 2;
  M += 2;

  REP(i,N)REP(j,M) safe[i][j] = false;
  REP(i,N)REP(j,M) _component[i][j] = -1;

  CNUM = 0;
  REP(i,N)REP(j,M) if (_component[i][j] == -1 && B[i][j] == 'W') {
    ++CNUM;
    _find[CNUM] = CNUM;
    find_components(i, j);
  }
  //
  // REP(i,N) {
  //   REP(j,M) if (B[i][j] == 'W') {
  //     printf("%d", component(i,j));
  //   } else {
  //     printf("%c", B[i][j]);
  //   }
  //   printf("\n");
  // }
  //
}

int __dfsvisit[2000][2000];
int __dfsid = 0;
int dfs(int r, int c) {
  if (__dfsvisit[r][c] == __dfsid || B[r][c] == 'W') return 0;
  __dfsvisit[r][c] = __dfsid;

  int result = B[r][c] == 'K';
  result += dfs(r+1,c) + dfs(r-1,c) + dfs(r,c-1) + dfs(r,c+1);
  return result;
}

void mark_safe(int r, int c) {
  if (__dfsvisit[r][c] == __dfsid || B[r][c] == 'W') return;
  __dfsvisit[r][c] = __dfsid;

  safe[r][c] = true;
  dfs(r+1, c);
  dfs(r-1, c);
  dfs(r, c-1);
  dfs(r, c+1);
}

PII operator+(PII a, PII b) {
  return {a.st + b.st, a.nd + b.nd};
}

vector<PII> n = {{-1, -1}, {-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}};
vector<int> dr = {-1, -1, -1, 0, 1, 1,  1,  0};
vector<int> dc = {-1,  0,  1, 1, 1, 0, -1, -1};

int were_safe = 0, count_with_check = 0, count_without_check = 1;

int NowaWarownia(int r, int c) {
  B[r][c] = 'W';
  if (safe[r][c]) {
    ++were_safe;
    return 1;
  }

  bool requires_check = false;
  for (int os = 1; os <= 7; os += 2) {
    for (int os2 = 1; os2 < os; os2 += 2) {
      if (B[r+dr[os]][c+dc[os]] == 'W' || B[r+dr[os2]][c+dc[os2]] == 'W') continue;
      // printf("CHECKING %d %d %d %d\n", dr[os], dc[os], dr[os2], dc[os2]);

      for (int w = (os+1)%8; w != os2; (w += 1) %= 8)
        for (int w2 = (os2+1)%8; w2 != os; (w2 += 1) %= 8) {
          // printf("(%d %d) (%d %d)\n", dr[w], dc[w], dr[w2], dc[w2]);
          if (B[r+dr[w]][c+dc[w]] != 'W' || B[r+dr[w2]][c+dc[w2]] != 'W') continue;

          if (component(r+dr[w], c+dc[w]) == component(r+dr[w2], c+dc[w2])) {
            requires_check = true;
          }
        }
    }
  }

  // printf("Requires check %d %d: %d\n", r, c, requires_check);

  bool ok = true;
  if (requires_check) ++count_with_check; else ++count_without_check;
  //
  // printf("%d %d %d\n", count_with_check, count_without_check, were_safe);

  if (requires_check) {
    ++__dfsid;
    for (int os = 1; os <= 7; os += 2) {
      int visited = dfs(r+dr[os], c+dc[os]);
      if (visited > 0 && visited < OSADY) {
        ok = false;
        break;
      }
    }

    if (ok) {
      int dfsing_after = __dfsid;

      for (int os = 1; os <= 7; os += 2) {
        if (__dfsvisit[r+dr[os]][c+dc[os]] > dfsing_after) {
          continue;
        }

        ++__dfsid;
        int visited = dfs(r+dr[os], c+dc[os]);
        if (visited == 0) {
          ++__dfsid;
          mark_safe(r+dr[os], c+dc[os]);
        }
      }
    }
  }

  if (ok) {
    ++CNUM;
    _find[CNUM] = CNUM;
    _component[r][c] = CNUM;
    REP(n, 8) if (B[r+dr[n]][c+dc[n]] == 'W') {
      component(r, c) = component(r+dr[n], c + dc[n]);
    }
    // REP(i,N) {
    //   REP(j,M) if (B[i][j] == 'W') {
    //     printf("%d", component(i,j));
    //   } else {
    //     printf("%c", B[i][j]);
    //   }
    //   printf("\n");
    // }

    return 1;
  } else {
    B[r][c] = '.';
    return 0;
  }
}

void PrzeniesOsade(int r1, int c1, int r2, int c2) {
  // Dla testów
  if (B[r1][c1] != 'K' || B[r2][c2] != '.') {
    return;
  }

  B[r1][c1] = '.';
  B[r2][c2] = 'K';
}