Reprint Please specify source: http://blog.csdn.net/u012860063?
Viewmode=contents
Topic Links:
id=1321 ">http://poj.org/problem?id=1321
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Welcome to Flair Lodge: Http://user.qzone.qq.com/593830943/main
--------------------------------------------------------------------------------------------------------------- -------------------------------------------
Description
In a given shape of the chessboard (the shape may be irregular) above the pieces of chess, chess pieces no difference.
The two pieces that are required to be placed should not be placed in the same row or column in the chessboard. Please programmatically solve the chessboard for a given shape and size. All possible placement of K-pieces of the plan C.
Input
The input contains multiple sets of test data.
The first line of data for each group is two positive integers. n K, separated by a space, indicates that the narration board will be described in a matrix of n*n. And the number of pieces to be placed. N <= 8, K <= N
When 1-1 indicates the end of the input.
The following n-line description describes the shape of the chessboard: N characters per line, where # indicates the board area. Represents an empty area (the data guarantees that no extra blank lines or blank columns appear).
Output
For each set of data, give a line of output, the output is placed in the number of programs C (Data Assurance c<2^31).
Sample Input
2 #.. #4 4...#. #.. #.. #...-1-1
Sample Output
21st
The code is as follows:
#include <iostream> #include <algorithm> #include <cstring>using namespace std; #define TM 17char Chess [TM] [Tm];int N, k;int ans = 0, col[tm];void dfs (int row, int num) {if (num = = k) {Ans++;return;} if (Row > N) return, for (int j = 1; J <= N; j + +) {if (chess[row][j] = = ' # ' &&!col[j]) {Col[j] = 1;dfs (row+1,num+1 ); col[j] = 0;}} DFS (row+1,num);//Assume the previous line cannot be placed. Search for the next line of return on Row+1;} int main () {while (cin>>n>>k) {ans = 0;if (n = =-1 && k = =-1) break;memset (col,0,sizeof (col)), for (int i = 1; I <= N; i++) {cin>>chess[i]+1;} DFS (1,0); Cout<<ans<<endl;} return 0;}
poj1321 Checkerboard problem (Deep search Dfs)