Home > Back-end >  To solve the c
To solve the c

Time:10-11

How can you speak input letter number (the letters have a space)
Example, type a b c d letter a can be replaced with the serial number 1

CodePudding user response:

#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;
}

CodePudding user response:

Can't use an array

CodePudding user response:

Depends on how you define the relationship between the letters and Numbers in the
Look from code x [I] is int, you cin input is the letter a, this is wrong, not the right to obtain input
Do you want to define char ch
Cin & gt;> Ch;
If (ch=='a') x [I]=1;
If a, b, c, d... Order of the corresponding number 1, 2, 3, 4
You can let
[I] x=ch - 'a' + 1;
  • Related