USACO sec1.5 Checker Challenge

八皇后问题,最大输入13,单case,时限1s;

和 HDOJ 不同的是:

Do not precalculate the value and print it (or even find a formula for it); that's cheating. Work on your program until it can solve the problem properly. If you insist on cheating, your login to the USACO training pages will be removed and you will be disqualified from all USACO competitions. YOU HAVE BEEN WARNED.

( 不知道真的假的)

/*
PROG : checker
LANG : C++
*/
# include <stdio.h>
# include <string.h>

# define N 15

int k, n = 0, m = 3;
/********************************/
int sol[N];
char vis[3][3*N];

void dfs(int cnt)
{
    int i, j;
    if (cnt == k)
    {
        ++n;
        if (m)
        {
            --m;
            printf("%d", sol[1]);
            for (i = 2; i <= k; ++i) printf(" %d", sol[i]);
            putchar('\n');
        }
    }
    else for (i = 1; i <= k; ++i)
        if (!vis[0][i] && !vis[1][cnt+i] && !vis[2][cnt-i+k])
        {
            vis[0][i] = vis[1][cnt+i] = vis[2][cnt-i+k] = 1;
            sol[cnt+1] = i;
            dfs(cnt+1);
            vis[0][i] = vis[1][cnt+i] = vis[2][cnt-i+k] = 0;
        }
}

/********************************/
void solve(void)
{
    scanf("%d", &k);
    memset(vis, 0, sizeof(vis));
    dfs(0);
    printf("%d\n", n);
}

int main()
{
    freopen("checker.in", "r", stdin);
    freopen("checker.out", "w", stdout);
    
    solve();
    
    fclose(stdin);
    fclose(stdout);
    
    return 0;
}
原文地址:https://www.cnblogs.com/JMDWQ/p/2648781.html