[Search] four-color problem

Original title Portal

Thinking


This question is not that hard, but this card konjac half an hour, because I forgot to make the total schemes plus return after a bloody lesson ...... ah QAQ In one approach, the search finished, certainly not after forget again ......

Code


#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<stack>
#include<map>
using namespace std;

int n,e[9][9],b[9],ans;

int r(int no)
{
    int i;
    for(i=1;i<=n;i++)
    {
        if(e[no][i]&&b[i]==b[no])
            return 0;
    }
    return 1;
}

void dfs(int no)
{
    if(no==n+1)
    {
        ans++;
        return;
    }   
    int i;
    for(i=1;i<=4;i++)
    {
        b[no]=i;
        if(r(no))
            dfs(no+1);
        b[no]=0;
    }
}

int main()
{
    int i,j;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            cin>>e[i][j];
        }
    }
    dfs(1);
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/gongdakai/p/11443252.html