如何使用python编程解决地图四色问题

发布网友 发布时间:2022-04-26 04:41

我来回答

1个回答

热心网友 时间:2022-04-18 02:50

# -*- coding: cp936 -*-
def FourColorLabel(GuanXiJuZheng):
    Num=len(GuanXiJuZheng)
    Color=[-1 for i in range(Num)]
    n=m=1
    #染色第一个区域,先设置为1
    while m<=Num:
        while n<=4 and m<=Num:
            flag=True
            for k in range(m-1):
                if GuanXiJuZheng[m-1][k]==1 and Color[k]==n:
                    flag=False    #染色有冲突
                    n+=1
                    break    
            if flag:
                Color[m-1]=n;
                m+=1
                n=1
        if n>4:                 # 超出标记范围 必须回退
            m-=1
            n=Color[m-1]+1
    return Color

GuanXiJuZheng=[
                [0,1,0,0,0,0,1],
                [1,0,1,1,1,1,1],
                [0,1,0,1,0,0,0],
                [0,1,1,0,1,0,0],
                [0,1,0,1,0,1,0],
                [0,1,0,0,1,0,1],
                [1,1,0,0,0,1,0]
             ]
for i in FourColorLabel(GuanXiJuZheng):
    print i

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com