四色定理涂色的解

题目:运用四色定理,为N个局域举行配色,颜色为1、2、3、4四种,另有数组adj[][N],如adj[i][j]=1则表示i区域与j区域相邻,数组color[N],如color[i]=1,表示i区域的颜色为1号颜色。

#include <stdio.h>  

static int curcolor=0;

int pickaunic(int map[6][6],int color[6],int i)
{
    for (int j=0;j<6;j++)
    {
        if (map[i][j]==1 && (curcolor%4) == color[j]) //如果区域i和区域j相邻并且当前要涂的颜色正好是区域j的颜色
        {
            curcolor++; //那么就换一种颜色,继续找下一个于区域i相邻的区域,如果颜色还冲突,那么再换颜色
        }
    }
    return (curcolor%4); //返回要涂的颜色
}

void colored(int map[6][6],int color[6],int i)
{
    if (color[i]>0)
    {
        return;
    }
    else
    {
        color[i] = pickaunic(map,color,i);
        for (int j=0;j<6;j++)
        {
            if (map[i][j]==1)
            {
                colored(map,color,j);
            }
        }
    }
}

int main() 
{     
    int map[6][6] = {{0, 0, 0, 1, 1, 0},
                     {0, 0, 1, 1, 0, 1},
                     {0, 1, 0, 1, 0, 0},
                     {1, 1, 1, 0, 1, 1},
                     {1, 0, 0, 1, 0, 1},
                     {0, 1, 0, 1, 1, 0}}; 
    int color[6]={-1,-1,-1,-1,-1,-1};
    
    colored(map,color,0);

    return 0;
}  
原文地址:https://www.cnblogs.com/darknightsnow/p/2706383.html