【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;
}
posted @ 2018-06-03 10:49  gwj1139177410  阅读(112)  评论(0编辑  收藏  举报
选择