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

#define rep(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>b;i--)
#define repLL(i,a,b) for(LL i=(LL)a;i<(LL)b;i++)
#define perLL(i,a,b) for(LL i=(LL)a;i>(LL)b;i--)
#define debug5(a,b,c,d,e) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<" "<<#e<<": "<<e<<endl;
#define debug4(a,b,c,d)  cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<" "<<#d<<": "<<d<<endl;
#define debug3(a,b,c) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<" "<<#c<<": "<<c<<endl;
#define debug2(a,b) cerr<<#a<<": "<<a<<" "<<#b<<": "<<b<<endl;
#define debug(a) cerr<<#a<<": "<<a<<endl;
#define pb push_back
#define mp make_pair
#define SZ(x) ((int)(x).size())
#define ALL(x) x.begin(),x.end()
#define fi first
#define se second
#define _upgrade ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

using namespace std;

typedef long long LL;
typedef pair<int,int> PII;
typedef pair<LL,LL> PLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
typedef vector<LL> VLL;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<char> VC;
typedef long double LD;
typedef pair<LD,LD> PLD;
typedef vector<LD> VLD;
typedef vector<PLD> VPLD;

const int maxn=(1e3)+7;
const int inf=(1e9)+7;
const LL LLinf=(1e18)+7;
const LD eps=1e-9;
const LL mod=1e9+7;

// ***************************** CODE ***************************** //

int n;
int m;
char dp[maxn][maxn];
bool vis[maxn][maxn];

VPII mov={{1,0},{-1,0},{0,1},{0,-1}};

void NowaWyspa(int N,int M,char ** board)
{
      n=N;
      m=M;
      rep(i,1,n+1)
            rep(j,1,m+1)
                  dp[i][j]=board[i-1][j-1];
      rep(i,0,n+5)
            rep(j,0,m+5)
                  vis[i][j]=1;
}

void PrzeniesOsade(int a,int b,int c,int d)
{
      dp[a][b]='.';
      dp[c][d]='K';
}

void dfs(int x,int y)
{
      vis[x][y]=1;
      for(auto s:mov)
      {
            int xx=x+s.fi;
            int yy=y+s.se;
            if(vis[xx][yy]==0 && dp[xx][yy]!='W')
                  dfs(xx,yy);
      }
}

int NowaWarownia(int x,int y)
{
      dp[x][y]='W';
      rep(i,1,n+1)
            rep(j,1,m+1)
                  vis[i][j]=0;
      bool f=0;
      rep(i,1,n+1)
            rep(j,1,m+1)
                  if(vis[i][j]==0 && dp[i][j]=='K')
                  {
                        dfs(i,j);
                        if(f)
                        {
                              dp[x][y]='.';
                              return 0;
                        }
                        f=1;
                  }
      return 1;
}