图的遍历算法-七巧板涂色

有如图所示的七巧板,试设计算法,使用至多4种不同的颜色对七巧板进行涂色(每块涂一种颜色),要求相邻区域的颜色互不相同,打印输出所有可能的涂色方案。

《图的遍历算法-七巧板涂色》


算法设计:

1、使用邻接矩阵表示七巧板的相邻情况

2、使用蛮力法进行搜索


最终结果:

《图的遍历算法-七巧板涂色》


代码:

#include <iostream>

using namespace std;

//三角板个数
const int n=7;
//邻接矩阵表,用来判断是否相邻
const int data[n][n] =
{
    {0,1,0,0,1,0,1},
    {1,0,0,1,0,1,0},
    {0,0,0,0,1,0,1},
    {0,1,1,0,0,1,1},
    {1,0,0,0,0,0,1},
    {0,1,0,1,0,0,0},
    {1,0,1,1,1,0,0}
};
//每个三角板的颜色
int color[n]= {0,0,0,0,0,0,0};

static int total;
void tryFind(int s);//查找涂色方案的递归
int colorSame(int s);//判断与周围的三角板颜色是否相同
void output();//输出涂色方案

int main()
{
    total=0;
    tryFind(0);
    cout<<"Total= "<<total<<endl;
    return 0;
}

void tryFind(int s)
{
    //s=0~6,如果s=7说明已经涂完
    if(s==n)
        output();
    else
    {
        //1、2、3、4代表四种颜色
        //只有与周围三角块的颜色不同时才会进入下一个三角板的涂色
        for(int i=1; i<=4; i++)
        {
            color[s]=i;
            if(colorSame(s)==0)
                tryFind(s+1);
        }
    }
}

int colorSame(int s)
{
    int flag=0;
    for(int i=0; i<s; i++)
    {
        //使用邻接矩阵判断是否相邻
        //如果相邻判断颜色是否相同
        if(data[i][s]==1 && color[i]==color[s])
            flag=1;
    }
    return flag;
}

void output()
{
    cout<<"serial number: ";
    for(int i=0; i<n; i++)
    {
        cout<<color[i]<<" ";
    }
    total++;
    cout<<endl;
}

    原文作者:数据结构之图
    原文地址: https://blog.csdn.net/double2hao/article/details/51635637
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞