Checkerboard Issues
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 26726 |
|
Accepted: 13242 |
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
Title Link: http://poj.org/problem?id=1321
Problem-solving ideas: DFS, line directly from the top down to the pawn, column with visit array needs to mark, particularly simple deep search, directly see the code can master.
The code is as follows:
#include <cstdio> #include <cstring>char a[10][10];int visy[10]; Tag column, guaranteed not to put the same column int n,m,ans;void dfs (int st,int cnt)//st mark the starting line, from the top down to the pawn {if (cnt==m) {Ans++;return;} for (int i=st;i<n;i++) {for (int j=0;j<n;j++) {if (!visy[j]&&a[i][j]== ' # ') {Visy[j]=1;dfs (i+1,cnt+1); visy[j]=0;}}}} int main () {while (scanf ("%d%d", &n,&m)!=eof&& (n!=-1| | M!=-1) {Ans=0;memset (visy,0,sizeof (Visy)), scanf ("%d%d", &n,&m), for (int i=0;i<n;i++) scanf ("%s", A[i]); DFS (0,0);p rintf ("%d\n", ans);} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1321 Checkerboard problem (simple Dfs)