Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

Re:神奇的构造法,把这个题变成了水题,DFS会T的

Posted by yinjian at 2016-07-29 09:55:37 on Problem 3239
In Reply To:神奇的构造法,把这个题变成了水题,DFS会T的 Posted by:yinjian at 2016-07-29 09:53:53
//贴一下DFS吧,虽然T了
#include <stdio.h>
#include <string.h>

int n;
int ans = 0;
int maps[305][305] = {0};

bool judge(int k,int i)
{
    for(int j=1; j<=n&&j!=i; j++)
        if(maps[k][j]==1)
            return false;

    for(int j=1; j<=n&&j!=k; j++)
        if(maps[j][i]==1)
            return false;

    for(int j=1; k+j<=n&&i+j<=n; j++)
    {
        if(maps[k+j][i+j]==1)
            return false;
    }

    for(int j=1; k-j>=1&&i-j>=1; j++)
    {
        if(maps[k-j][i-j]==1)
            return false;
    }

    for(int j=1; k-j>=1&&i+j<=n; j++)
    {
        if(maps[k-j][i+j]==1)
            return false;
    }

    for(int j=1; k+j<=n&&i-j>=1; j++)
    {
        if(maps[k+j][i-j]==1)
            return false;
    }

    return true;
}

bool dfs(int k)
{
    if(k>n)
    {
        ans ++;
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(maps[i][j])
                    printf("%d",j);
            }
        }
        printf("\n");
        return true;
    }

    for(int i=1; i<=n; i++)
    {
        if(judge(k,i))
        {
            maps[k][i] = 1;
            if(dfs(k+1))
                return true;
            maps[k][i] = 0;
        }
    }
    return false;
}

int main()
{
    while(scanf("%d",&n),n)
    {
        memset(maps,0,sizeof(maps));
        for(int i=1; i<=n; i++)
        {
            maps[1][i] = 1;
            if(dfs(2))
                break;
            maps[1][i] = 0;
        }
        //printf("%d\n",ans);
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator