P1123 c++ dfs 谁来帮我看看dfs结束条件是啥(有注解)

P1123 取数游戏

# 我推荐你可以看一眼我的代码。 ```c #include <bits/stdc++.h> using namespace std; const int N = 10; int n,m,ans,sum; int w[N][N]; int a[N][N]; bool check(int x, int y){ for(int i =x - 1;i <= x + 1;i++){ for(int j = y - 1;j <= y + 1;j++){ if(a[i][j] == 1) return false; } } return true; } void dfs(int x, int y,int sum){ if(x == n + 1){ ans = max(ans,sum); return; } int cx = x, cy = y + 1; if(cy > m){ cx++; cy = 1; } if(check(x,y)){ a[x][y] = 1; dfs(cx,cy,sum + w[x][y]); } a[x][y] = 0; dfs(cx,cy,sum); } int main(){ int T; cin >> T; int i,j; while(T--){ cin >> n >> m; for(i = 1;i <= n;i++){ for(j = 1;j <= m;j++){ cin >> w[i][j]; } } ans = 0; dfs(1,1,0); cout << ans << endl; } return 0; } ```
by will0703 @ 2023-05-27 17:04:14


|