中文题目不解释. 思路:枚举每个可以放置的点,对该点进行深搜.满足题意方案数加1. 代码: #include <iostream> #include <cstring> using namespace std; int n,k; bool map[9][9]; bool row[9],col[9]; int ans; void dfs(int d, int r) { int i,j; if(d==k) { ans++; return; } for(i=r+1; i<=n; i++) for(j=1; j<=n; j++) { if(map[i][j] && row[i]==0 && col[j]==0) { row[i] = 1; col[j] = 1; dfs(d+1,i); row[i] = 0; col[j] = 0; } } } int main(void) { while(cin >> n >> k) { memset(map,0,sizeof(map)); memset(row,0,sizeof(row)); memset(col,0,sizeof(col)); ans = 0; if(n==-1 && k==-1) break; int i,j; char ch; for(i=1; i<=n; i++) for(j=1; j<=n; j++) { cin >> ch; if(ch=='#') map[i][j] = 1; } int r = n-k+1; for(i=1; i<=r; i++) for(j=1; j<=n; j++) if(map[i][j]) { row[i] = 1; col[j] = 1; dfs(1,i); row[i] = 0; col[j] = 0; } cout << ans << endl; } // end of while return 0; }