2 solutions
-
3
#include<bits/stdc++.h> using namespace std; int n, k, num, l[20], cnt, p;//l[]标记列 char b[10][10]; void search(int p)//对每一层搜索 { if(num==k)//符合条件,计数加一 { cnt++; return; } if(p>=n) return;//越界返回 for(int i = 0; i<n; i++) { if(!l[i]&&b[p][i]=='#') { l[i]=1; num++; search(p+1);//选了这一层//回溯 num--; l[i]=0; } } search(p+1);//没选这一层 } int main() { while(cin>>n>>k) { cnt=0; if(n==-1&&k==-1) break; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) { cin>>b[i][j]; } } search(0); cout<<cnt<<endl; } return 0; }
-
2
#include <bits/stdc++.h> using namespace std; int n, k, ans; char c[10][10]; bool visx[10], visy[10]; int a[10][10]; void dfs(int x, int y, int cnt){ if (x > n){ x = 1, y++; } if (y > n){ if (cnt == k){ ans++; } return ; } if (c[x][y] == '#' && !visx[x] && !visy[y]){ visx[x] = visy[y] = true; a[x][y] = 1; dfs(x + 1, y, cnt + 1); a[x][y] = 0; visx[x] = visy[y] = false; dfs(x + 1, y, cnt); } else{ dfs(x + 1, y, cnt); } } int main(){ while (cin >> n >> k && n != -1 && k != -1){ ans = 0; for (int i = 1; i <= n; i++){ for (int j = 1; j <= n; j++){ cin >> c[i][j]; } } dfs(1, 1, 0); cout << ans << endl; } return 0; }
- 1
Information
- ID
- 703
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 7
- Tags
- # Submissions
- 92
- Accepted
- 21
- Uploaded By