棋盤問題
| Time Limit: 1000MS | | Memory Limit: 10000K |
| Total Submissions: 95463 | | Accepted: 43553 |
Description
在一個給定形狀的棋盤(形狀可能是不規(guī)則的)上面擺放棋子,棋子沒有區(qū)別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列网缝,請編程求解對于給定形狀和大小的棋盤佛致,擺放k個棋子的所有可行的擺放方案C。
Input
輸入含有多組測試數(shù)據(jù)衷笋。
每組數(shù)據(jù)的第一行是兩個正整數(shù),n k,用一個空格隔開疲扎,表示了將在一個n*n的矩陣內(nèi)描述棋盤,以及擺放棋子的數(shù)目捷雕。 n <= 8 , k <= n
當(dāng)為-1 -1時表示輸入結(jié)束椒丧。
隨后的n行描述了棋盤的形狀:每行有n個字符,其中 # 表示棋盤區(qū)域救巷, . 表示空白區(qū)域(數(shù)據(jù)保證不出現(xiàn)多余的空白行或者空白列)壶熏。
Output
對于每一組數(shù)據(jù),給出一行輸出浦译,輸出擺放的方案數(shù)目C (數(shù)據(jù)保證C<2^31)棒假。
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
Source
蔡錯@pku
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn = 8 + 5;
char mp[maxn][maxn];
bool vis[maxn];
typedef long long ll;
ll n, k, ans;
int dfs(int x, int y) {
if(y >= k) {
ans++;
return 0;
}
for(int i = x; i < n; i++) {
for(int j = 0; j < n; j++) {
if(!vis[j] && mp[i][j] == '#') {
vis[j] = true;
dfs(i + 1, y + 1);
vis[j] = false;
}
}
}
return 0;
}
int main() {
while(cin >> n >> k) {
if(n == -1 && k == -1) break;
memset(vis, false, sizeof(vis));
memset(mp, 0, sizeof(mp));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++)
cin >> mp[i][j];
ans = 0;
dfs(0, 0);
cout << ans << '\n';
}
return 0;
}