原书问题,可以转换为图的m着色问题 ,下面该问题的代码

这里有参考ppt与code,免积分载

http://download.csdn.net/detail/u011467621/6341195

// 1.9.cpp : Defines the entry point for the console application.
// #include "stdafx.h"
#include "iostream"
#include "stdio.h" using namespace std; bool ColoringGraph(int G[][],int n,int m);
bool IsOk(int G[][],int color[],int k,int n) ;
int G[][]={{,,,,},{,,,,},{,,,,},{,,,,},{,,,,}};
int _tmain(int argc, _TCHAR* argv[])
{
int n=,m=;
ColoringGraph(G,n,m);
return ;
} bool ColoringGraph(int G[][],int n,int m)
//n是图的定点个数,G是图的连接矩阵,Gij=1说明i定点与j定点有连接。m是最多可以上的色
//若输出有效上色,返回1,否则返回0
{
int *color=new int[n]; for(int i=;i<n;i++)//给每个顶点颜色初始化为0
color[i]=; int k=; while(k>=)//k代表顶点个数,当k回溯到0说明已经没有可行解了
{
while(color[k]<=m)
{
color[k]++; if (IsOk(G,color,k,n))
break; }
if(color[k]<=m && k==n-)
{
cout<<"OK";
for(int j=;j<n;j++)
cout<<color[j];
return ;
}
else if(color[k]<=m && k<n-)
{
k++;
}
else//若k>m,说明此路不同回溯到上一层
{ color[k]=;
k--;
} } return ;
} bool IsOk(int G[][],int color[],int k,int n) //判断是否有相同色的顶点,与之是一个边的
{
for(int i=;i<n;i++)
{
if(G[i][k]== && color[i]==color[k])
return false;
}
return true;
}

参考文献:

http://wenku.baidu.com/view/d7242fd1c1c708a1284a444d.html

欢迎拍砖,交流。感谢您的阅读,若您想支持一下本文,麻烦顶一下,您的鼓励,是我的动力,谢谢!- -
欢迎转载本文,

转载时请附上本文地址:http://www.cnblogs.com/Dzhouqi/p/3346466.html
另外:欢迎访问我的博客 http://www.cnblogs.com/Dzhouqi/

编程之美:1.9高效率安排见面会 图的m着色问题 回溯法的更多相关文章

  1. 2017“编程之美”终章:AI之战勇者为王

    编者按:8月15日,第六届微软“编程之美”挑战赛在选手的火热比拼中圆满落下帷幕.“编程之美”挑战赛是由微软主办,面向高校学生开展的大型编程比赛.自2012年起,微软每年都在革新比赛命题.紧跟时代潮流, ...

  2. 编程之美2014挑战赛 复赛 Codehunt平台试题答案

    var appInsights=window.appInsights||function(config){ function r(config){t[config]=function(){var i= ...

  3. LeetCode:Climbing Stairs(编程之美2.9-斐波那契数列)

    题目链接 You are climbing a stair case. It takes n steps to reach to the top. Each time you can either c ...

  4. 编程之美2.5:寻找最大的K个数

    编程之美2.5:寻找最大的K个数 引申:寻找第k大的数: 方法一: // 选择第k大的数(通过改进快速排序来实现) public static void SelectShort(int[] array ...

  5. 24点C++程序实现 编程之美1.16

    解法1,对于任意输入的四个数字,给出一个24点的解法,若无解,则没有输出. 原理参照下图(编程之美原书) 代码如下,仅供参考 // 1.16.cpp : Defines the entry point ...

  6. Python编程之美:最佳实践指南PDF高清完整版免费下载|百度云盘|Python新手到进阶

    百度云盘:Python编程之美:最佳实践指南PDF高清完整版免费下载 提取码:1py6 内容简介 <Python编程之美:最佳实践指南>是Python用户的一本百科式学习指南,由Pytho ...

  7. 生成CPU使用率 sin 曲线 控制cpu使用率 编程之美

    入职Oracle 以后想着把之前写过的<编程之美>中控制CPU使用率曲线的程序再写一边, 可是总是由于入职须要学习的东西太多, 没有时间. 程序早就写好了. 最终有机会贴出来了.o(∩∩) ...

  8. 编程之美Q1

    题目 和数书页有点类似,就直接数吧 #include<iostream> using namespace std; class q1 { public: size_t func(size_ ...

  9. 编程之美2015初赛第一场 hihoCoder #1156 : 彩色的树(染色问题)

    #1156 : 彩色的树 时间限制:2000ms 单点时限:1000ms 内存限制:256MB 描述 给定一棵n个节点的树,节点编号为1, , …, n.树中有n - 1条边,任意两个节点间恰好有一条 ...

随机推荐

  1. ubuntu grub 引导修复

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4167644.html   (1) 先使用ls命令,找到Ubuntu的安装分区: 在 grub ...

  2. color 颜色代码 android res/values/colors.xml

    <?xml version="1.0" encoding="utf-8"?> <resources> <color name=&q ...

  3. 【Qt】数据库连接池

    请查看公孙二狗的文章 数据库连接池

  4. grails环境搭建

    关于grails,前方有大坑,入坑需谨慎. 使用grails,最好有人指点,因为有很多坑等着你去跳.如果完全是自己折腾,每前进一步都会花一些时间,且不说这些时间用来干其他事情有更多回报,光是像堵车一样 ...

  5. php三维数组去重(示例代码)

    php三维数组去重的示例代码.  假设叫数组 $my_array; <?php // 新建一个空的数组. $tmp_array = array(); $new_array = array(); ...

  6. Spark Tungsten揭秘 Day3 内存分配和管理内幕

    Spark Tungsten揭秘 Day3 内存分配和管理内幕 恭喜Spark2.0发布,今天会看一下2.0的源码. 今天会讲下Tungsten内存分配和管理的内幕.Tungsten想要工作,要有数据 ...

  7. WPF中的WebBrowser

    MainWindow.xaml.cs //新窗口事件 { Uri uri = new Uri(textBox_uri.Text); System.Windows.Forms.Integration.W ...

  8. Sharing

    To store English words, one method is to use linked lists and store a word letter by letter. To save ...

  9. N皇后问题2

    Description Examine the  checkerboard below and note that the six checkers are arranged on the board ...

  10. IPHONE开发知识

    IPHONE开发知识http://www.cnblogs.com/valensoft/archive/2010/06/09/1754836.htmlhttp://www.cocoachina.com/ ...