poj1321 DFS水
2011年6月03日 22:09
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 11335 | Accepted: 5563 |
Description
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
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 | #include<cstdio> #include<cstring> int n,k; int i; int j; char board[12][12]; int pos[12]; int cnt; void DFS( int row, int num) { if (num==0) { cnt++; return ; } if (row>=n) return ; for ( int l=0; l<n; l++) { if (pos[l]!=1&&board[row][l]== '#' ) { pos[l]=1; DFS(row+1,num-1); pos[l]=0; } } DFS(row+1,num); } int main() { scanf ( "%d%d" ,&n,&k); while (n!=-1&&k!=-1) { for (i=0; i<n; i++) scanf ( "%s" ,board[i]); cnt=0; memset (pos,0, sizeof (pos)); DFS(0,k); printf ( "%d\n" ,cnt); scanf ( "%d%d" ,&n,&k); } return 0; } |