POJ 1321 Checkerboard Problem (DFS)

Source: Internet
Author: User
Tags first row
Topic links

poj1321 Topics

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

Sample Output
2
1 Analysis

This problem feeling is the "eight queen question" of the weakening version, simple DFS topic AC.
Idea: Store the Board area position in an array, and then each checkerboard area either places a piece, or does not put a pawn, deep search (equivalent to generate the number of permutations).
For a chessboard area can play chess pieces, like the eight queens, we use two Boolean array to maintain, r[i] to indicate whether there is a pawn in line I, C[j] Indicates whether there is a pawn in column J.
The deep search function maintains two parameters:
1. The current search is in the first several board area
2. Total number of pieces currently in play
Add 1 Code when =k the total number of board to be put

#include <iostream> #include <cstdio> #include <cstring> using namespace std; struct Node {int x, y;}
The p[100];//p array is used to hold the position of each checkerboard area bool r[10],c[10]; 
int Cnt,k,ans;
        void dfs (int num,int sum)//num indicates that the board area number is currently being searched, and sum indicates that the number of discs currently in play {if (sum==k)//conforms to a scheme {ans++;
    Return
    } if (num==cnt) return;
        if (R[p[num+1].x]&&c[p[num+1].y]) {r[p[num+1].x]=c[p[num+1].y]=false;  DFS (NUM+1,SUM+1); Place Pawn R[p[num+1].x]=c[p[num+1].y]=true in the next checkerboard area; Backtrack} dfs (num+1,sum);
    Do not place a pawn in the next checkerboard field} int main () {int n,i,j;
    Char ch;
        while (scanf ("%d%d", &n,&k)!=eof) {if (n==-1&&k==-1) break;
        cnt=0;
            for (i=1;i<=n;i++) for (j=1;j<=n;j++) {cin>>ch;
                if (ch== ' # ') {Node temp;
                Temp.x=i;
                Temp.y=j; P[++cnt]=temp;
      CNT is the total number of checkerboard regions}  } ans=0;
        Memset (r,true,sizeof (R));
        Memset (C,true,sizeof (c));
        R[p[1].x]=false;c[p[1].y]=false; DFS (a);
        The first checkerboard area puts chess memset (r,true,sizeof (R));
        Memset (C,true,sizeof (c)); DFS (1,0);
    The first board area does not play chess cout<<ans<<endl;
} 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.