「Bzoj 1715」「Usaco2006 Dec」Wormholes 虫洞 (SPFA找负环)

BZOJ 1715
Luogu 2850
from: USACO 2006 Dec Gold
由题意就是判一个负环,用SPFA,据说DFS要快一点,不过这里还是BFS了。
一个点进队超过$n$次就存在负环。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stack>
#include<vector>
#define ms(i, j) memset(i, j, sizeof i)
#define LL long long
using namespace std;
const int MAXN = 100 + 5;
int n;
char s[MAXN][MAXN];
void clear() {
}
void init() {
clear();
for (int i=1;i<=n;i++) scanf("%s", s[i]+1);
}
void solve() {
int ans = 0;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
for (int x=i+1;x<=n;x++)
for (int y=1;y<=j;y++) {
int si = j - y, ti = x - i;
int x3 = i + si, y3 = j + ti, x4 = x + si, y4 = y + ti;
if (x3<=0||x3>n||x4<=0||x4>n||y3<=0||y3>n||y4<=0||y4>n) continue;
if (s[i][j]=='B'||s[x][y]=='B'||s[x3][y3]=='B'||s[x4][y4]=='B') continue;
int tot = 0;
if (s[i][j]=='J') tot++;
if (s[x][y]=='J') tot++;
if (s[x3][y3]=='J') tot++;
if (s[x4][y4]=='J') tot++;
if (tot>=3) ans = max(ans, si*si+ti*ti);
}
printf("%d\n", ans);
}
int main() {
#ifndef ONLINE_JUDGE
freopen("1.in", "r", stdin);freopen("1.out", "w", stdout);
#endif
while (scanf("%d", &n)==1&&n) init(), solve();
return 0;
}
------ 本文结束 ------