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
| #include<bits/stdc++.h> using namespace std; const int maxn = 10;
int l,n,ans; int a[maxn][maxn], row[maxn], col[maxn];
bool judge(){ for(int i = 1 ; i <= n ; i ++){ if(row[i] != l || col[i] != l) return false; } return true; }
void dfs(int x, int y){ for(int i = 1 ; i <= n ; i ++){ if(col[i] > l || row[i] > l) return; } if(x == n && y == n){ for(int i = 0 ; i <= l; i ++){ a[x][y] = i; row[x] += i; col[y] += i; if(judge()) ans++; row[x] -= i; col[y] -= i; } return; } for(int i = 0 ; i <= l; i ++){ a[x][y] = i; if(y == n){ row[x] += i; col[y] += i; if(row[x] == l) dfs(x + 1, 1); row[x] -= i; col[y] -= i; }else{ row[x] += i; col[y] += i; if(!(x == n && col[y] != l)) dfs(x, y + 1); row[x] -= i; col[y] -= i; } } }
int main(void){ cin>>l>>n; dfs(1,1); cout<<ans; return 0; }
|