Home > Back-end >  To solve the map coloring problem code changes
To solve the map coloring problem code changes

Time:10-11

#include
#include
#include
#include
#include
#include
using namespace std;

Const int MAXX=200;//set the boundary
Int map [MAXX] [MAXX];//adjacency matrix storage figure
Int x [MAXX];//color color number
Int sum=1;//package number
int n;//the number of vertices
int m;//the number of edge
Int color_nums=4;//color number
Char a, [40].//vertex

Void createmap ()//create the adjacency matrix
{
Int u;//the vertices 1
Int v.//vertex 2
Memset (map, 0, sizeof (map));
for (int i=1; i<=m; I++)
{
Cin & gt;> U> V.
The map [u] [v]=1;
The map [v] [u]=1;
}
}

Bool OK (int t)//judgment is the same color number
{
for(int j=1; j{
If (map [t] [j])//if connected
{
If (x==[j] x [t])//and the colour is the same as if
{
return false;//returns false, color change, try to
}
}
}
return true; No.//if the color is not the same is true
}

Void backtrack int (t)//back, recursive function
{
If (t> N& & Sum==1)//reach a leaf node
{
Sum++;//package number
cout <"Dyeing solution for" & lt; for (int i=1; i<=n; I++)
{
cout }

}
The else
{
for (int i=1; i<=color_nums; I++)//try to other color number
{
X [t]=I;//record lubricious date
If (OK (t))//if there is no repeat color number
{
Backtrack (t + 1);//recursive continue down
}
}
}
}


Int main ()
{
Cin & gt;> M> n;//input vertex number
Createmap ();
Backtrack (1);
return 0;
}
  • Related