"POJ" 1321 (dfs--board problem)

Source: Internet
Author: User

Board problem

Time Limit: 1000MS Memory Limit: 10000K
Total submissions: 59850 accepted: 28687

Description a chess board in a given shape (the shape may be irregular), with no distinction between the pieces. 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 group of data, give a row of outputs, output of the scheme number C (data guarantee c<2^31).

Sample Input

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

Sample Output

2
1

This problem is written by oneself, the thought may not be very clear (cover face
Main.cpp//dfs--checkerboard problem///Created by Showlo on 2018/4/19. COPYRIGHT©2018 year Showlo.
All rights reserved.
#include <stdio.h> #include <algorithm> using namespace std;
Char map[10][10];
int vis[10];
int N,k,num,ans;
    void Dfs (int row) {int i,j;
    if (row==n+1) {return; else for (i=0; i<n; i++) {if (map[row][i]== ' # ' &&vis[i]==0) {//print
                F ("%d\n", I);
                Vis[i]=1;
                num++;
                    if (num==k) {ans++;
                    printf ("s%d\n", ans);
                    vis[i]=0;
                    num--;
                Continue
                For (j=row+1 j<=row+1+ (n-k); j + +) Dfs (j);
            num--;
}}//num--;
    int main () {int i; while (scanf ("%d%d", &n,&k)!=eof) {if (n==-1| |
        K==-1) {break; memset (Vis, 0, sizeof (VIS));
        memset (map, 0, sizeof (map));
        num=ans=0;
        For (i=0 i<n; i++) {scanf ("%s", Map[i]);
        for (i=0; i<=n-k; i++) {DFS (i);
    printf ("%d\n", ans);
return 0; }

Here's a code that's a little bit clearer:

From: https://blog.csdn.net/tigerisland45/article/details/51926290

Main.cpp//dfs--board problem. 1////Created by Showlo on 2018/4/19. COPYRIGHT©2018 year Showlo.
All rights reserved.
/* POJ1321 Board Issue * * #include <stdio.h> #include <memory.h> #define MAXN 8 char GRID[MAXN][MAXN];
int VISIT[MAXN];
int MAXPlan;

int N, K, Count;
    
    DFS: Probing row row, col column void Dfs (int row) {int I, J;
    
    if (row >= N) return; For (i=0 i<n; i++) {if (grid[row][i] = = ' # ' && visit[i] = = 0) {//Find the line's can            Play chess place, at the same time put the chess of this column did not pass the piece count++;
            When placed, the Count plus 1 if (count = = k) {maxplan++;   else {Visit[i] = 1;
                When placed, set to 1 for (j=row+1; j<=row+1+ (N-k), J + +) from the next row until the N-k+1 row Dfs (j) is left;   Visit[i] = 0;            When backtracking, set to 0} count--;
    
    When backtracking, the count is restored}} int main (void) {int i; while (scanf ("%d%d", &n, &k)!= EOF) {GetChar ();
        
        Determine the end condition if (n = = 1 && k = = 1) break;
        
        Read data for (i=0; i<n; i++) gets (grid[i));
        Depth First search memset (visit, 0, N);
        MAXPlan = 0;
        Count = 0;
        
        for (i=0; i<=n-k; i++) Dfs (i);
    Output results printf ("%d\n", MAXPlan);
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.