-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMUSKET.cpp
64 lines (59 loc) · 1.78 KB
/
MUSKET.cpp
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
// Ivan Carvalho
// Solution to https://www.spoj.com/problems/MUSKET/
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 210;
int dp[MAXN][MAXN][MAXN], N, matriz[MAXN][MAXN], possivel[MAXN];
char entrada[MAXN];
inline int converte(int x) {
if (x > N) return x - N;
return x;
}
int solve(int k, int i, int j) {
if (dp[k][i][j] != -1) return dp[k][i][j];
if (k < i && k > j) return dp[k][i][j] = 0;
if (k == i) {
if (i == j) return dp[k][i][j] = 1;
for (int l = i + 1; l <= j; l++) {
if (matriz[converte(k)][converte(l)] * solve(l, i + 1, j))
return dp[k][i][j] = 1;
}
return dp[k][i][j] = 0;
} else if (k == j) {
for (int l = j - 1; l >= i; l--) {
if (matriz[converte(k)][converte(l)] * solve(l, i, j - 1))
return dp[k][i][j] = 1;
}
return dp[k][i][j] = 0;
} else {
return dp[k][i][j] = solve(k, i, k) && solve(k, k, j);
}
}
int main() {
int TC;
scanf("%d", &TC);
while (TC--) {
scanf("%d", &N);
memset(dp, -1, sizeof(dp));
memset(matriz, 0, sizeof(matriz));
memset(possivel, 0, sizeof(possivel));
for (int i = 1; i <= N; i++) {
scanf("%s", entrada);
for (int j = 1; j <= N; j++) {
matriz[i][j] = (entrada[j - 1] == '1');
}
}
for (int ini = 1, fim = N; ini <= N; ini++, fim++) {
for (int i = 1; i <= 2 * N; i++) {
possivel[converte(i)] |= solve(i, ini, fim);
}
}
int ans = 0;
for (int i = 1; i <= N; i++) ans += possivel[i];
printf("%d\n", ans);
for (int i = 1; i <= N; i++) {
if (possivel[i]) printf("%d\n", i);
}
}
return 0;
}