Title Link: POJ 1321 checkerboard Problem
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 28825 |
|
Accepted: 14276 |
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
A search for the topic, there is open two array dx[],dy[], to search for the stereotype thinking, in fact, that is used to walk the maze.
The title requires the selection of the pieces do not walk, different columns, so we can search a row, found the # go straight to the next line
This first guarantee the different lines, and then open a vis "" array, used to record the number of pieces in the column, deep search can be.
"Source Code"
#include <cstdio> #include <iostream> #include <cstring>using namespace Std;int N,k,ans;char map[10][ 10];bool vis[10];void dfs (int cur,int cnt) {if (cnt==k) {ans++; return;} if (cur>n) //If more than the last line of return, for (; cur<=n;cur++)//Start search for (int j=1;j<=n;j++) {if (map[cur][j]== ' # ') starting from the first row &&!vis[j]) {Vis[j]=1;dfs (cur+1,cnt+1); vis[j]=false;//backtracking}}}int main () {while (scanf ("%d%d", &n,&k)! = Eof&&n!=-1&&k!=-1) {memset (vis,0,sizeof (VIS)); for (int. i=1;i<=n;i++) for (int j=1;j<=n;j++) { scanf ("%c", &map[i][j]);} Ans = 0;dfs (1,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 (search)