Description
In a given shape of the chessboard (the shape may be irregular) on the top of the pieces, chess pieces no difference. If you need to place any of the two pieces in the same row or column in the chessboard, please program the chessboard with a given shape and size, and put all the feasible arrangement C for the K pieces.
Input
The input contains multiple sets of test data.
The first row of each set of data is two positive integers, n K, separated by a space, indicating that the chessboard will be described in a n*n matrix and the number of pieces to be placed. N <= 8, K <= N
When 1-1 indicates the end of the input.
The next n lines describe the shape of the chessboard: N characters per line, where # represents 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
Idea: Mark the column whether there are pieces, and then search by line;
#include <cstdio>#include<algorithm>#include<iostream>#include<queue>#include<cstring>using namespacestd;Chardata[ A][ A];intc,n,k;intvisit[ A];intto[4][2]={{0,1},{0,-1},{1,0},{-1,0}};voidDfsintXintCNT) { if(cnt==k) {C++;return ; } for(inti=x;i<=n;i++) for(intj=1; j<=n;j++) { if(data[i][j]=='#'&&visit[j]==0) {Visit[j]=1; DFS (i+1, cnt+1); VISIT[J]=0; } }}intMain () { while(cin>>n>>k) {if(n==-1&&k==-1) Break; for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) Cin>>Data[i][j]; C=0; DFS (1,0); cout<<c<<Endl; } return 0;}
POJ 1321 Checkerboard Problem Dfs