forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathE.cpp
More file actions
190 lines (170 loc) · 5.4 KB
/
E.cpp
File metadata and controls
190 lines (170 loc) · 5.4 KB
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
#include <sstream>
#include <iomanip>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <string>
#include <deque>
#include <complex>
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; i++)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; i--)
#define REP(i,a) for(int i=0,_a=(a); i<_a; i++)
#define FORN(i,a,b) for(int i=(a),_b=(b);i<_b;i++)
#define DOWN(i,a,b) for(int i=a,_b=(b);i>=_b;i--)
#define SET(a,v) memset(a,v,sizeof(a))
#define sqr(x) ((x)*(x))
#define ll long long
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define DEBUG(x) cout << #x << " = "; cout << x << endl;
#define PR(a,n) cout << #a << " = "; FOR(_,1,n) cout << a[_] << ' '; cout << endl;
#define PR0(a,n) cout << #a << " = "; REP(_,n) cout << a[_] << ' '; cout << endl;
using namespace std;
//Buffer reading
int INP,AM,REACHEOF;
#define BUFSIZE (1<<12)
char BUF[BUFSIZE+1], *inp=BUF;
#define GETCHAR(INP) { \
if(!*inp) { \
if (REACHEOF) return 0;\
memset(BUF,0,sizeof BUF);\
int inpzzz = fread(BUF,1,BUFSIZE,stdin);\
if (inpzzz != BUFSIZE) REACHEOF = true;\
inp=BUF; \
} \
INP=*inp++; \
}
#define DIG(a) (((a)>='0')&&((a)<='9'))
#define GN(j) { \
AM=0;\
GETCHAR(INP); while(!DIG(INP) && INP!='-') GETCHAR(INP);\
if (INP=='-') {AM=1;GETCHAR(INP);} \
j=INP-'0'; GETCHAR(INP); \
while(DIG(INP)){j=10*j+(INP-'0');GETCHAR(INP);} \
if (AM) j=-j;\
}
//End of buffer reading
const long double PI = acos((long double) -1.0);
#define TWO(x) (1<<(x))
#define CONTAIN(S,x) (S & TWO(x))
;
typedef pair< pair<int,int>, int > State;
char a[16][16], b[16][16];
bool ok[16][16];
int id[16][16], posu[16], posv[16];
int m, n, cnt;
queue< State > q;
int d[16][16][17000];
bool inside(int u, int v) {
return u > 0 && u <= m && v > 0 && v <= n;
}
const int knighti[] = {-2,-2,-1,-1,1,1,2,2};
const int knightj[] = {-1,1,-2,2,-2,2,-1,1};
const int rooki[] = {-1,1,0,0};
const int rookj[] = {0,0,-1,1};
const int bishopi[] = {-1,-1,1,1};
const int bishopj[] = {-1,1,-1,1};
bool solve() {
while (!q.empty()) {
State cur = q.front(); q.pop();
if (cur.S == 0) {
printf("%d\n", d[cur.F.F][cur.F.S][cur.S]);
return true;
}
FOR(i,1,m) FOR(j,1,n) {
if (a[i][j] == '*' || a[i][j] == '.') b[i][j] = '.';
else {
if (CONTAIN(cur.S, id[i][j])) {
b[i][j] = a[i][j];
}
else b[i][j] = '.';
}
}
memset(ok, true, sizeof ok);
FOR(i,1,m) FOR(j,1,n) {
if (b[i][j] == 'K') {
REP(dir,8) {
int ii = i + knighti[dir], jj = j + knightj[dir];
if (inside(ii, jj)) ok[ii][jj] = false;
}
}
else if (b[i][j] == 'R') {
REP(dir,4) {
int ii = i + rooki[dir], jj = j + rookj[dir];
while (inside(ii, jj)) {
ok[ii][jj] = false;
if (b[ii][jj] != '.') break;
ii += rooki[dir], jj += rookj[dir];
}
}
}
else if (b[i][j] == 'B') {
REP(dir,4) {
int ii = i + bishopi[dir], jj = j + bishopj[dir];
while (inside(ii, jj)) {
ok[ii][jj] = false;
if (b[ii][jj] != '.') break;
ii += bishopi[dir], jj += bishopj[dir];
}
}
}
}
// cout << cur.F.F << ' ' << cur.F.S << ' ' << cur.S << " " << d[cur.F.F][cur.F.S][cur.S] << endl;
// FOR(i,1,m) {
// PR(b[i], n);
// }
// FOR(i,1,m) {
// PR(ok[i], n);
// }
FOR(di,-1,1) FOR(dj,-1,1) if (di || dj) {
State next = MP(MP(cur.F.F + di, cur.F.S + dj), cur.S);
if (inside(next.F.F, next.F.S) && ok[next.F.F][next.F.S]) {
int now = id[next.F.F][next.F.S];
if (now >= 0 && CONTAIN(next.S, now)) {
next.S -= TWO(now);
}
if (d[next.F.F][next.F.S][next.S] < 0) {
d[next.F.F][next.F.S][next.S] = d[cur.F.F][cur.F.S][cur.S] + 1;
q.push(next);
}
}
}
}
return false;
}
int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
while (scanf("%d%d\n", &m, &n) == 2) {
while (!q.empty()) q.pop();
cnt = 0;
memset(id, -1, sizeof id);
FOR(i,1,m) {
scanf("%s\n", &a[i][1]);
FOR(j,1,n) if (a[i][j] != '*' && a[i][j] != '.') {
id[i][j] = cnt;
posu[cnt] = i;
posv[cnt] = j;
++cnt;
}
}
memset(d, -1, sizeof d);
FOR(i,1,m) FOR(j,1,n)
if (a[i][j] == '*') {
q.push(MP(MP(i, j), TWO(cnt)-1));
d[i][j][TWO(cnt)-1] = 0;
}
if (!solve()) puts("-1");
}
return 0;
}