poj 1321( 搜尋 )
阿新 • • 發佈:2019-02-19
棋盤問題
每組資料的第一行是兩個正整數,n k,用一個空格隔開,表示了將在一個n*n的矩陣內描述棋盤,以及擺放棋子的數目。 n <= 8 , k <= n
當為-1 -1時表示輸入結束。
隨後的n行描述了棋盤的形狀:每行有n個字元,其中 # 表示棋盤區域, . 表示空白區域(資料保證不出現多餘的空白行或者空白列)。
對於每一組資料,給出一行輸出,輸出擺放的方案數目C (資料保證C<2^31)。
Time Limit: 1000MS | Memory Limit: 10000K |
Total Submissions: 14396 | Accepted: 7100 |
Description
在一個給定形狀的棋盤(形狀可能是不規則的)上面擺放棋子,棋子沒有區別。要求擺放時任意的兩個棋子不能放在棋盤中的同一行或者同一列,請程式設計求解對於給定形狀和大小的棋盤,擺放k個棋子的所有可行的擺放方案C。Input
輸入含有多組測試資料。每組資料的第一行是兩個正整數,n k,用一個空格隔開,表示了將在一個n*n的矩陣內描述棋盤,以及擺放棋子的數目。 n <= 8 , k <= n
當為-1 -1時表示輸入結束。
隨後的n行描述了棋盤的形狀:每行有n個字元,其中 # 表示棋盤區域, . 表示空白區域(資料保證不出現多餘的空白行或者空白列)。
Output
Sample Input
2 1 #. .# 4 4 ...# ..#. .#.. #... -1 -1
Sample Output
2 1
Source
題目型別:搜尋 題目描述:略 題目分析:略 程式碼如下:#include <stdio.h> #include <string.h> #define N 10 _Bool cvisit[N]; char map[N][N]; int k,total,n; void dfs(int level,int count){ int i; if( k == count ){ total++; return; } if( level == n){ return; } if( count + n - level < k){ //剪枝 return; } for( i = 0 ; i < n ; i++){ if( map[level][i] == '#' && !cvisit[i]) { cvisit[i] = 1; dfs(level+1,count+1); cvisit[i] = 0; } } dfs(level+1,count); return; } int main() { int i; while(scanf("%d%d",&n,&k), n != -1 || k != -1){ memset(cvisit,0,sizeof(cvisit)); total = 0; for( i = 0 ; i < n; i++){ scanf("%s",map[i]); } dfs(0,0); printf("%d\n",total); } return 0; }