虽说博弈,还是找规律题,-1周围dfs一次,然后在外层dfs一次,把-1的边界全都变为1就行了
#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> int s[310][310]; bool vis[310][310], arr[310][310]; int n, m, p; int xx[] = {0, 1, 0, -1}; int yy[] = {-1, 0, 1, 0}; bool found; void dfs(int x, int y){ if (x == 0 || y == 0 || x == n + 1 || y == m + 1){ found = true; return; } if(vis[x][y]) return; vis[x][y] = true; if(s[x][y] >= 1) return; for (int i = 0; i < 4; i++) dfs(x + xx[i], y + yy[i]); } void tarjan(int x, int y){ if (x == 0 || y == 0 || x == n + 1 || y == m + 1) return ; if(arr[x][y]) return; arr[x][y] = 1; if(!vis[x][y]) p += s[x][y]; else { if(s[x][y] >= 2) p += s[x][y] - 1; return; } for (int i = 0; i < 4; i++) tarjan(x + xx[i], y + yy[i]); } int main() { // freopen("in.txt", "r", stdin); while (scanf("%d%d",&n,&m) != EOF) { int opx, opy; for(int i = 0; i <= n + 1; ++i) for(int j = 0; j <= m + 1; ++j) vis[i][j] = arr[i][j] = 0; for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++){ scanf("%d", &s[i][j]); if (s[i][j] == -1){ opx = i; opy = j; } } found = false; dfs(opx, opy); if(found){ puts("Ali Win"); continue; } p = 0; for(int i = 1; i <= n; ++i) { if(!arr[i][1]) tarjan(i, 1); if(!arr[i][m]) tarjan(i, m); } for(int i = 2; i < m; ++i){ if(!arr[1][i]) tarjan(1, i); if(!arr[n][i]) tarjan(n, i); } if(p % 2 == 1) puts("Ali Win"); else puts("Baba Win"); } return 0; }