[POJ - 1321] chessboard (dfs)

Chessboard

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

Test input comprising a plurality of sets of 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

Topic links:

https://vjudge.net/problem/POJ-1321

AC Code:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <fstream>
 4 #include <algorithm>
 5 #include <cmath>
 6 #include <deque>
 7 #include <vector>
 8 #include <queue>
 9 #include <string>
10 #include <cstring>
11 #include <map>
12 #include <stack>
13 #include <set>
14 #define mod 1000000007
15 #define ll long long
16 #define INF 0x3f3f3f3f
. 17  the using  namespace STD;
 18 is  char MP [ 15 ] [ 15 ];
 . 19  int VIS [ 15 ]; // column 
20 is  int n-, K;
 21 is  int SUM = 0 ;
 22 is  void DFS ( int X, int CNT) // X for the current row, cnt for the current number of pieces of display 
23 is  {
 24      IF (CNT == K)
 25      {
 26 is          SUM ++ ;
 27      }
 28      for (int I = X; I <n-; I ++ )
 29      {
 30          for ( int J = 0 ; J <n-; J ++ )
 31 is          {
 32              IF (MP [I] [J] == ' # ' && VIS [J] == 0 ) // determines not passed this column 
33 is              {
 34 is                  VIS [J] = . 1 ;
 35                  DFS (I + . 1 , CNT + . 1 );
 36                  VIS [J] = 0 ;
 37 [              }
 38 is          }
 39     }
 40  }
 41 is  int main ()
 42 is  {
 43 is      the while (CIN >> >> n- K)
 44 is      {
 45          IF (n-== - . 1 && K == - . 1 )
 46 is              BREAK ;
 47          the else 
48          {
 49              // every to update 
50              SUM = 0 ;
 51 is              Memset (VIS, 0 , the sizeof (VIS));
 52 is              for ( int I = 0 ; I <n-; I ++ )
 53 is                 for(int j=0; j<n; j++)
54                     cin >> mp[i][j];
55             dfs(0,0);
56         }
57         cout<<sum<<endl;
58     }
59 }

 

Guess you like

Origin www.cnblogs.com/sky-stars/p/10963005.html