POJ 1321 chessboard problem (deep search) _algorithm

Source: Internet
Author: User
Tags first row

POJ 1321 Board question (deep search)
Total time limit: 1000ms memory limit: 65536kB

Describe
In a given shape of the chessboard (the shape may be irregular) above the pieces, the pieces are no different. When required to put any two pieces can not be placed in the chessboard of the same row or the same column, please programmatically solve for the given shape and size of the chessboard, put k pieces of all feasible placement scheme C.

Input
Input contains multiple sets of test data.
The first row of each set of data is two positive integers, n K, separated by a single space, indicating that the chessboard will be described in a n*n matrix and the number of pieces placed. N <= 8, K <= N
When-1-1 indicates the end of the input.
The following n lines describe the shape of the chessboard: there are n characters per line, where # represents the Checkerboard area. Represents a blank area (data guarantees that no extra blank lines or blank columns appear).

Output
For each set of data, give a line of output, output of the scheme number C (data guarantee c<2^31).

Sample input
2 1
#.
.#
4 4
...#
.. #.
.#..
#...
-1-1

Sample output
2
1

Source
Cai @pku

Deep Search, the idea is very clear, the only question is, how to judge a position legal. The simplicity of each of the flags to prevent the placement of the same row position of the chess is a waste of serious, reference to the eight Queens problem optimization, only need to mark a column whether to let go (because a row search does not appear a row of two problems).

Accepted    256kB   40ms    603 B   g++ 7 minutes ago
 #include <stdio.h> int n,k int ans; int map[9][9],column[9]; char ch;
        void Dfs (int now,int k) {if (now==n+1 | | k==0) {if (k==0) ans++;
    Return
    DFS (NOW+1,K);
            for (int i=1;i<=n;i++) if (map[now][i]==0 &&!column[i]) {column[i]=1;
            DFS (NOW+1,K-1);
        column[i]=0; 
} return; int main () {while (scanf ("%d%d\n", &n,&k) && (n!=-1 && k!=-1)) {for (int i=1;i&
                lt;=n;i++) {for (int j=1;j<=n;j++) {scanf ("%c", &ch);
            map[i][j]= (ch== '. ');
        } scanf ("\ n");
        } ans=0; 
        for (int i=1;i<=n;i++) column[i]=0;
        DFS (1,K);
    printf ("%d\n", ans);
return 0; }

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.