Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6342 Accepted: 3061
Description
In a checkerboard given shape (the shape may be irregular) placed above the pieces, pieces no difference. The requirements of any two pieces can not be placed in the same row or the same column in the board when the display is required, program for solving a given board size and shape, placing the k pieces of all possible placement scheme C.
Input
Input contains multiple test data.
The first line of each data are two positive integers, NK, separated by a space, and indicates the number of the board will be described in a matrix of n * n, and put the pieces. n <= 8, k <= n
when the end of input is represented by -1 -1.
Then n lines describe the shape of a checkerboard: n characters per line, where # represents the board area, indicates a blank area (extra blank line data is guaranteed not to appear or blank columns).
Output
For each set of data, one line of output is given, the number of output display program C (data guarantee C <2 ^ 31).
Sample Input
2 1
#.
.#
4 4
...#
..#.
.#..
#...
-1 -1
Sample Output
2
1
Source
Cai wrong @pku
//
#include < iostream >
using namespace std;
int DFS( char board[ 9 ][ 9 ], int N, int s, int k)
{
if (k == 0 ) return 1 ;
int cnt = 0 ;
for ( int i = s; i < N; ++ i)
for ( int j = 0 ; j < N; ++ j)
if (board[i][j] == 0 )
{
for ( int k = 0 ; k < N; ++ k){ ++ board[k][j]; ++ board[i][k];}
cnt += DFS(board,N,i + 1 , k - 1 );
for ( int k = 0 ; k < N; ++ k){ -- board[k][j]; -- board[i][k];}
};
return cnt;
}
int main( int argc, char * argv[])
{
char board[ 9 ][ 9 ];
int N, K;
while (scanf( " %d %d\n " , & N, & K) && N != - 1 && K != - 1 )
{
for ( int i = 0 ; i < N; ++ i) gets(board[i]);
for ( int i = 0 ; i < N; ++ i)
for ( int j = 0 ; j < N; ++ j)
board[i][j] = (board[i][j] == ' # ' ) ? 0 : 1 ;
cout << DFS(board,N, 0 ,K) << endl;
}
return 0 ;
}
Reproduced in: https: //www.cnblogs.com/asuran/archive/2009/10/10/1580180.html