Checkerboard Issues
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 24958 |
|
Accepted: 12333 |
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
Deep Search + backtracking method. here is the solution to the N-Queen problem, that is, a one-dimensional array c,c[i]=j to indicate that line I, j column already has;There is also the need to pay attention to backtracking back to the c[] mark.
#include <stdio.h> #include <string.h> #include <algorithm>using namespace Std;typedef __int64 ll;int Vis[10][10],map[10][10],c[10],n,k,tot;char m[10][10];//search void dfs (int cur,int cnt) {int i,j;if (cnt==k) {//put enough K when tot+ 1tot++;return;} if (n-cur+1<k-cnt) return,//The remaining line is insufficient to put enough k to return directly if (cur>n) return; int ok=0;for (i=1;i<=n;i++) if (Map[cur][i]) {OK =1;break;} First determine if the Cur line has a position that can be placed if (OK) {for (; i<=n;i++) {int ok2=1;if (Map[cur][i]) {for (j=1;j<cur;j++) if (c[j]==i) {ok2=0; break;} if (OK2) {c[cur]=i;//is taken here is the method of the N queen, c[i]=j means put on line I, Column J Dfs (cur+1,cnt+1); c[cur]=0;//reply Mark, backtracking}}}}dfs (cur+1,cnt); return ;} int main () {int i,j;while (scanf ("%d%d", &n,&k)!=eof) {if (n==-1 && k==-1) break;memset (map,0,sizeof (map )); for (i=1;i<=n;i++) for (j=1;j<=n;j++) {scanf ("%c", &m[i][j]), if (m[i][j]== ' # ') map[i][j]=1;} Memset (C,0,sizeof (c)), Tot=0;dfs (1,0);p rintf ("%d\n", tot);} return 0;}
POJ 1321 Checkerboard Problem deep search + backtracking