中国地图着色源代码

转自百度文库:http://wenku.baidu.com/link?url=1awdq8I04M-DS9l6hEBl1cwTPTPEDQ6uzXLKAbi7iNCLNNnkDDMpn0VkLgblptmhhqawDpBgAOORY5iZ052k1av78fobytpD2U3W9JhZITu&from_mod=copy_login

#include <iostream>
#include <string>
#include <time.h>
using namespace std;
#define MaxSize 34
#define N 4
int color[10]={0};
string name[34]={"广西","广东","云南","贵州","湖南","江西","福建",
              "浙江","安徽","湖北","重庆","四川","西藏","青海",
           "新疆","甘肃","陕西","宁夏","内蒙","北京","黑龙江",
     "吉林","辽宁","天津","河北","山西","河南","江苏",
              "山东","上海","海南","台湾","香港","澳门"};
// 1、广西; 2、广东; 3、云南; 4、贵州; 5、湖南; 6、江西; 7、福建;
// 8、浙江; 9、安徽;10、湖北;11、重庆;12、四川;13、西藏;14、青海;
//15、新疆;16、甘肃;17、陕西;18、宁夏;19、内蒙;20、北京;21、黑龙江;
//22、吉林;23、辽宁;24、天津;25、河北;26、山西;27、河南;28、江苏;
//29、山东;30、上海;31、海南;32、台湾;33、香港;34、澳门;
int shengguanxi[35][35]={{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//0
 {0,0,1,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//1
 {0,1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1},//2
 {0,1,0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//3
 {0,1,0,1,0,1,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//4
 {0,1,1,0,1,0,1,0,0,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//5
 {0,0,1,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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//33
 {0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},//34
};

class ChinaMap
{
public:
 ChinaMap(){}
 ChinaMap(string SM[],int t,int b);
 ~ChinaMap(){}
    void ShowCM();
    int IsColorSame(int n);
 void ColorZL();
 void ShowColor();
 void ShangSe(int n);
private:
 string ShengMing[MaxSize+1];
    int ShengGX[MaxSize+1][MaxSize+1];
 int tNum,bNum;
};

ChinaMap::ChinaMap(string SM[],int t,int b)
{
 int i,j;
 tNum=t;
 bNum=b;
 for (i=1;i<=tNum;i++)
 {
  ShengMing[i]=SM[i-1];
 }
 for(i=0;i<=tNum;i++)
  for (j=0;j<=tNum;j++)
     ShengGX[i][j]=shengguanxi[i][j];
}
void ChinaMap::ShowCM()
{
 int i;
 cout<<"图的各顶点:"<<endl;
 for(i=1;i<=tNum;i++)
 { 
  cout<<ShengMing[i]<<"  ";
  if(i%7==0)
   cout<<endl;
 }
 cout<<endl;
/* cout<<"图的邻接矩阵:"<<endl;
 for (i=1;i<=tNum;i++)
 {
  for (j=1;j<=tNum;j++)
     cout<<ShengGX[i][j]<<"  ";
  cout<<endl;
 }*/
}

int ChinaMap::IsColorSame(int n)
{
 int i,flag=0;
 for (i=1;i<=n-1;i++)
 {
  if (ShengGX[i][n]==1&&color[i]==color[n])
  {
   flag=1;
   break;
  }
 }
 return flag;
}
void ChinaMap::ColorZL()
{
 int i;
    for (i=1;i<=tNum;i++)
    {
  cout<<ShengMing[i]<<":";
  switch (color[i])
  {
  case 1:cout<<"红色"<<"  ";
   break;
  case 2:cout<<"蓝色"<<"  ";
   break;
  case 3:cout<<"黄色"<<"  ";
   break;
  case 4:cout<<"绿色"<<"  ";
   break;
  case 5:cout<<"白色"<<"  ";
   break;
  case 6:cout<<"黑色"<<"  ";
   break;
  case 7:cout<<"橙色"<<"  ";
   break;
  default:
   break;
  }

if(i%7==0)
   cout<<endl;
    }
}
void ChinaMap::ShowColor()
{
// int i;
// for(i=1;i<=tNum;i++)
//  cout<<color[i]<<"  ";
 ColorZL();
 cout<<endl;
}
void ChinaMap::ShangSe(int n)
{
 int i;
 if (n>tNum)
 {
  ShowColor();
  exit(1);
 }
 else
 {
  for (i=1;i<=N;i++)
  {
   color[n]=rand()%N+1;
   if (IsColorSame(n)==0)
   {
    ShangSe(n+1);
   }
  }
 }
}
int main()

{
 srand((unsigned)time(NULL));
 
 ChinaMap cm(name,34,70); 
 cm.ShowCM();
 cout<<"着色方案:"<<endl;
 cm.ShangSe(1);
 return 0;
}

  • 2
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值