Los Eight Queens Valley P1219

Put all of this in 251 seconds, bet on! - "game of life zero"

Topic: https://www.luogu.org/problem/P1219

Eight Queens is a very very very classic deep search + go back problems.

This question is important to correct the idea. We naturally can not define a two-dimensional array and then loop judge there - this will definitely blow up.

So what methods?

mark.

To each column, the value of the diagonal line marked point to determine where the next Can.

This example, the two points are labeled row 1, 2-1 + 6 obliquely lower right diagonal rows are labeled 1, 2 + 1 obliquely lower left diagonal line marked or 1.

Then we can get this code.

a[i]=t;
b[i-t+n]=t; 
c[i+t]=t;

Finally put this to search deep inside, plus the back, can the AC.

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,ans=0;
int d[15],num=0;
//map<int,int>a,b,c;
int a[50],b[50],c[50];
void output()
{
    for(int i=1;i<=n;i++)
      printf("%d ",d[i]);
    the printf ( " \ n- " ); 
} 
void DFS ( int T) 
{ 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF !! (A [I] && B [I-T + n-] &&! C [T + I]) 
        { 
            a [I] = T;
 //             B [IT] = T; // because it can be used not control map is positive or negative 
            [IT + n-] B = T; 
            C [ I + T] = T; 
            D [T] = I;
             IF (T == n-) 
            { 
                IF (NUM ++ <= . 3) output();
                ans++;
            }
            else dfs(t+1);
            a[i]=0;
//            b[i-t]=0;
            b[i-t+n]=0;
            c[t+i]=0;
            d[t]=0;
        }
    }
}
int main()
{
    scanf("%d",&n);
    dfs(1);
    printf("%d\n",years);
    return  0 ; 
}

Moreover, because the representation is very clear odd diagonal, so you can see Can map, but because of some metaphysical reason, map time complexity higher, TLE will fall two points, thus adding special judge to complete.

#include<iostream>
#include<map>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int n,ans=0;
int d[15],num=0;
map<int,int>a,b,c;
void output()//输出 
{
    for(int i=1;i<=n;i++)
      printf("%d ",d[i]);
    printf(" \ N- " ); 
} 
void DFS ( int T) 
{ 
    for ( int I = . 1 ; I <= n-; I ++ ) 
    { 
        IF ! (A [I] && B [IT] && C [T +!! I]) 
        { 
            a [I] = T; 
            B [I -t] = T; // because it can be used not control map is positive or negative 
            C [I + T] = T; 
            D [T] = I; // simple labeled 
            IF (T == n-) 
            { 
                IF (NUM ++ <= . 3 ) Output (); 
                ANS ++;
            }
            else dfs(t+1);
            a[i]=0;
            b[i-t]=0;
            c[t+i]=0;
            d[t]=0;//回溯 
        }
    }
}
int main()
{
    scanf("%d",&n);
    if(n==12) 
    {
        printf("1 3 5 8 10 12 6 11 2 7 9 4\n");
        printf("1 3 5 10 8 11 2 12 6 9 7 4\n");
        printf("1 3 5 10 8 11 2 12 7 9 4 6\n14200");
        return 0;
    }
    if(n==13)
    {
        printf("1 3 5 2 9 12 10 13 4 6 8 11 7\n");
        printf("1 3 5 7 9 11 13 2 4 6 8 10 12\n");
        printf("1 3 5 7 12 10 13 6 4 2 8 11 9\n73712");
        return 0;
    }
    dfs(1);
    printf("%d\n",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/fjnhyzcrx-Mayuri/p/11360991.html