Poj1321 board Problem Solving report

Source: Internet
Author: User
Classification: DFS, maze, board Author: acshiryu time: 2011-7-24 address: acshiryu's blog board Problem
Time limit:1000 ms Memory limit:10000 K
Total submissions:11884 Accepted:5833

Description

There is no difference in placing a piece on a given-shape chessboard (which may be irregular. It is required that any two pawns should not be placed in the same row or column of the board. Please program to solve all feasible placement schemes C for all K pawns with a given shape and size.

Input

The input contains multiple groups of test data.
The first row of each group of data is two positive integers, n k, separated by a space, indicating that the board will be described in an N * n matrix, and the number of pieces placed. N <= 8, k <= N
If it is-1-1, the input ends.
The next n lines describe the shape of the Board: each line has n characters, where # indicates the board area, and .. indicates the blank area (no extra blank rows or columns are required for data ).

Output

For each group of data, a single line of output is provided, and the number of solutions placed in the output is C (Data guarantee is C <2 ^ 31 ).

Sample Input

2 1#..#4 4...#..#..#..#...-1 -1

Sample output

21
Simple search: The question is very clear. '#' can be used as a pawn, '.' is not allowed, and two pawns cannot be placed in the same row or column,
Two Methods of playing data one game ('*' indicates playing chess pieces)
*.      #.
. # Or .*
There is only one case for data 2
...*
..*.
.*..
*...
You only need to perform a deep search for this question. Each time you start to find the place where you can place the chess piece from the next line of the place where the chess piece is placed, when this point is found, record the number of rows and update the board, it will be updated '. '. If no part is found, the system returns the result. When all the pieces are put up, the system finds a link and counts more than 1. In this way, the search ensures that the AC

Reference code:

1 # include <iostream>
2 # include <cstdlib>
3 # include <cstdio>
4 # include <cstring>
5 # include <algorithm>
6 # include <cmath>
7 using namespace STD;
8 struct prog {
9 char map [10] [10]; // chessboard
10 int X; // The number of rows of the previous pawn.
11 };
12 INT m; // board size
13 int ans; // NUMBER OF SOLUTIONS
14 void DFS (Prog TMP, int N)
15 {
16 if (n = 0)
17 {// if all the required pawns have been put, the solver finds the answer, counts + 1, and returns
18 ans ++;
19 return;
20}
21 int I, J;
22 For (I = TMP. x + 1; I <= m-N; I ++)
23 {// search from the next row of the last pawn. If the number of remaining pawns is significantly greater than the number of remaining ones, you can know that there is no solution at this time.
24 For (j = 0; j <m; j ++)
25 {// search for the last one from the first chessboard of the row
26 if (TMP. Map [I] [J] = '#')
27 {// if you find a piece, you can play it.
28 prog tmp2;
29 tmp2 = TMP;
30 tmp2.x = I; // write down the number of rows to be changed
31 int K;
32 For (k = I + 1; k <m; k ++)
33 {// update the checkerboard. Because it does not search for the row or the checkerboard before the row, it is only used to update the checkerboard in the same column of the row.
34 tmp2.map [k] [J] = '.';
35}
36 DFS (tmp2, n-1); // place a piece to continue searching
37
38}
39}
40}
41}
42 int main ()
43 {
44
45 int N; // The number of required pawns
46 While (CIN> m> n, m! =-1 | n! =-1)
47 {
48 ans = 0; // Initialization is 0
49 prog map;
50 map. x =-1; // at this time, the pawn has not been placed, and the initialization is-1.
51 int I;
52 for (I = 0; I <m; I ++)
53 {
54 CIN> map. Map [I];
55}
56 DFS (MAP, N );
57 cout <ans <Endl;
58}
59 return 0;
60}

  

Contact Us

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.