This repository was archived by the owner on Jan 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathfairy_chess.cpp
More file actions
88 lines (64 loc) · 2.14 KB
/
fairy_chess.cpp
File metadata and controls
88 lines (64 loc) · 2.14 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
// https://www.hackerrank.com/challenges/fairy-chess
#include <iostream>
#include <vector>
using namespace std;
const int MODULO = 1000000007;
void get_ways() {
int N, M, S; cin >> N >> M >> S;
vector< vector<int> > A(2 * N + 1, vector<int>(2 * N + 1, 0));
vector< vector<int> > ways(2 * N + 1, vector<int>(2 * N + 1, 0));
for (int i = 0; i < N; ++i) {
string S; cin >> S;
for (int j = 0; j < N; ++j) {
if (S[j] == 'P') continue;
int x = i + j + 1;
int y = N - i + j;
A[x][y] = 1;
if (S[j] == 'L') ++ways[x][y];
}
}
auto add = [](int &value, int amount) {
value += amount;
if (value >= MODULO) value -= MODULO;
if (value < 0) value += MODULO;
};
for (int i = 0; i < M; ++i) {
vector< vector<int> > past(2 * N + 1, vector<int>(2 * N + 1, 0));
swap(past, ways);
for (int j = 0; j < 2 * N + 1; ++j)
for (int k = 0; k < 2 * N + 1; ++k) {
if (j > 0)
add(past[j][k], past[j - 1][k]);
if (k > 0)
add(past[j][k], past[j][k - 1]);
if (j > 0 and k > 0)
add(past[j][k], -past[j - 1][k - 1]);
}
for (int j = 0; j < 2 * N + 1; ++j)
for (int k = 0; k < 2 * N + 1; ++k) {
if (A[j][k] == 0) continue;
int x1 = max(j - S, 0);
int x2 = min(j + S, 2 * N);
int y1 = max(k - S, 0);
int y2 = min(k + S, 2 * N);
ways[j][k] = past[x2][y2];
if (x1 > 0)
add(ways[j][k], -past[x1 - 1][y2]);
if (y1 > 0)
add(ways[j][k], -past[x2][y1 - 1]);
if (x1 > 0 and y1 > 0)
add(ways[j][k], past[x1 - 1][y1 - 1]);
}
}
int output = 0;
for (int i = 0; i < 2 * N + 1; ++i)
for (int j = 0; j < 2 * N + 1; ++j)
add(output, ways[i][j]);
cout << output << endl;
}
int main() {
int T;
cin >> T;
while (T--) get_ways();
return 0;
}