离散数学 c++ 地图涂色

来源:互联网 发布:java如何调用数组 编辑:程序博客网 时间:2024/06/02 14:25


#include <iostream>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
enum color{red,orange,yellow,green,blue,pan,white,black};
struct province
{
  string name;
  //the name of province;
    color col;
  //the color of province;
  vector<province> negbor;
  //the negbor of province
 void add(province &p)
  {
   this->negbor.push_back(p);
   //add a negbor province for this province
   p.negbor.push_back((*this));
   //when it add a negbor province,this well become the negbor for another province negbor
   cout<<"adding...."<<endl;
  }
//add negbor for province;
};
string toString(province &p)
{
 string str="";
 switch(p.col)
 {
      case 0 :str="red";
    break;
   case 1 :str="orange";
    break;
   case 2 :str="yellow";
    break;
   case 3:str="green";
    break;
   case 4 :str="blue";
    break;
   case 5 :str="pan";
    break;
   case 6 :str="white";
    break;
      case 7 :str="black";
    break;
   default:str="can not find the color";
    break;
 }
 return str;
}
void giveColor(province &p)
{
 int temp=0;

//research the colors of it's neighour
  color co=static_cast<color>(rand()%8);
 for(int i=0;i<p.negbor.size();i++)
 {
  if (p.negbor[i].col!=co)
  {
   temp++;
  }
  else
  {
   continue;
  }
  if (temp==p.negbor.size())
  {
   cout<<"the number of  "<<p.name<<"'s negbor province:"<<p.negbor.size()<<endl;
   p.col=co;
   cout<<"the color of "<<p.name<<" is: "<<toString(p)<<endl;
  }
  else
  {
   co=static_cast<color>(rand()%8);
  }
 }
 
}
int main()
{
 cout<<"for example there are 6 province on a map:p1,p2,p3,p4,p5,p6"<<endl;
 province p1,p2,p3,p4,p5,p6;
 p1.name="p1";
 p2.name="p2";
 p3.name="p3";
 p4.name="p4";
 p5.name="p5";
 p6.name="p6";
 p5.col=pan;
 p1.add(p5);
 p4.add(p5);
   p3.add(p5);
    p2.add(p5);
 p2.add(p3);
 p4.add(p1);
 p6.add(p2);
 giveColor(p1);
 giveColor(p2);
 giveColor(p3);
 giveColor(p4);
 giveColor(p5);
 giveColor(p6);
  system("pause");
return 0;
}

0 0
原创粉丝点击