1321: Board question (depth-first search Advanced)

Topic links: http://bailian.openjudge.cn/practice/1321?lang=en_US

Meaning of the questions:

  Seeking in a matrix of n * n k-pieces of board placed embodiment, these pieces are not the same column in the same row are not to

Ideas: first search may be several possible ways to search for each line piece with other pieces placed by depth.

code show as below:

#include <stdio.h> 
#include < String .h> 
#include <the iostream>
 the using  namespace STD;
 char MP [ 10 ] [ 10 ];
 int VIS [ 10 ]; // record whether each line piece searched 
int SUM , n-, K, CNT;
 void DFS ( int H) 
{ 
    IF (CNT == K) 
    { 
        SUM ++; // satisfies the conditional program number plus one 
        return ; 
    } 
    IF (H> K) // boundary condition 
    return ;
     for( Int I = 0 ; I <n-; I ++ ) 
    { 
        IF (VIS [I] && MP [H] [I] ==! ' # ' ) 
        { 
            VIS [I] = . 1 ; 
            CNT ++; // the condition piece number plus one. 
            DFS (H + . 1 ); // then the next line discharge piece 
            VIS [I] = 0 ; // If the search fails, or the number of pieces deep enough, the revocation flag. 
            cnt-- ; 
        } 
    } 
    DFS (H + . 1 ); // start deep search on the next line. 
}
 Int main (void)
{
    while(cin>>n>>k)
    {
        if(n==-1&&k==-1)
        break;
        sum=0;//初始化方案数 
        cnt=0;
        memset(vis,0,sizeof vis);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            cin>>mp[i][j];
        } 
    } 
    DFS ( 0 ); // start the search from the first row. 
    the printf ( " % D \ n- " , SUM); 
} 
    return  0 ; 
}

Summary about:

  This is a classic problem of deep searching questions, but started doing might not do that without thinking, read several times the question, is not to look at other bloggers blog, This question can still AC's.

Guess you like

Origin www.cnblogs.com/YHH520/p/12230326.html