【codevs1116】四色问题

problem

solution

codes

#include<iostream>
using namespace std;
int n, ans=0, c[10], e[10][10];
bool ok(int pos){
    for(int i = 0; i < pos; i++)
        for(int j = 0; j < pos; j++)
            if(e[i][j] && c[i]==c[j])return false;
    return true;
}
void dfs(int cur){
    if(cur == n) ans++;
    else for(int i = 1; i <= 4; i++){
        c[cur] = i;
        if(ok(cur+1))dfs(cur+1);
    }
}
int main(){
    cin>>n;
    for(int i = 0; i < n; i++)
        for(int j = 0; j < n; j++)
            cin>>e[i][j]; 
    dfs(0);
    cout<<ans;
}
原文地址:https://www.cnblogs.com/gwj1314/p/9444719.html