# kb-01-a< simple Search--dfs Eight Queens problem variant >

Source: Internet
Author: User

Title 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********************************************The subject is a variant of the eight Queens, Dfs deep search, than the eight queen simple point, only ask for the only row, diagonal no requirements, the order of the search is, the first line to start, put or not put in this line, put to M personal return, search in the end return;Code:`
`1#include <iostream>2#include <cstdio>3#include <cstring>4#include <vector>5 using namespacestd;6 Chara[Ten][Ten];7 intans=0, vis[Ten]={0};8 intn,m,t;9 voidDfsintXintk)Ten { One      if(k==m) A      { -ans++; -          return; the      } -      if(x>=N) -          return ; -       for(intI=0; i<n;i++) +      { -          if(a[x][i]=='#'&&vis[i]==0) +          { Avis[i]=1; atDFS (x+1, K +1);//put in the line, enumerate the positions that can be placed; -vis[i]=0; -          } -      } -DFS (x+1, k);//not put on the line; - } in intMain () - { to      while(SCANF ("%d%d", &n,&m)! =EOF) +     { -         if(n==-1|| m==-1) the              Break; *          for(intI=0; i<n;i++) \$scanf"%s", A[i]);Panax Notoginsengans=0; -memset (Vis,0,sizeof(Vis)); theDfs0,0); +cout<<ans<<Endl; A     } the     return 0; +}`
View Code

kb-01-a< simple Search--dfs Eight Queens problem variant >

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

## A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• #### Sales Support

1 on 1 presale consultation

• #### After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.