# 智子 的博客

### 题解 SP196 【MUSKET - Musketeers】

posted on 2019-07-28 16:22:52 | under 题解 |

## 定义状态

$dp_{i,j}$ 为第 $i$ 人与第 $j$ 人是否能够相遇

## 状态转移方程

$$dp_{i,j} = dp_{i,k} \&\&\ dp_{k,j} \&\&\ (w_{i,k} || w_{j,k})$$

## 代码

#include<iostream>
#include<cstring>
using namespace std;

const int MAXN = 100 * 2 + 5;

int w[MAXN][MAXN], f[MAXN][MAXN];
int n;

int main() {
int t;

cin >> t;
while(t--) {
memset(f, 0, sizeof(f)); //数组清零，我在这里掉了两次坑
memset(w, 0, sizeof(w));

cin >> n;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
char c;
cin >> c;
w[i][j] = c - '0';
w[i + n][j + n] = w[i + n][j] = w[i][j + n] = w[i][j];
}
}

for(int l = 1; l <= n + 1; l++) {
for(int i = 1; i + l - 1 <= n * 2; i++) {
int j = i + l - 1;

if(l <= 2) {
f[i][j] = 1; //边界条件
continue;
}

for(int k = i; k <= j; k++) {
if(f[i][k] && f[k][j] && (w[i][k] || w[j][k])) {
f[i][j] = 1;
break;
}
}
}
}

int ans = 0;
for(int i = 1; i <= n; i++) {
if(f[i][i + n]) {
ans++;
}
}
cout << ans << endl;
for(int i = 1; i <= n; i++) {
if(f[i][i + n]) {
cout << i << endl;
}
}
}

return 0;
}