-
Notifications
You must be signed in to change notification settings - Fork 11
/
图着色问题.cpp
50 lines (48 loc) · 1.19 KB
/
图着色问题.cpp
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include<bits/stdc++.h>
using namespace std;
const int n = 5;
int arc[n][n]= {{0,1,1,0,0},{1,0,1,1,1},{1,1,0,0,1},{0,1,0,0,1},{0,1,1,1,0}};
int color[n] = {0};
void GraphColor(int m);
int Ok(int k);
int main()
{
GraphColor(3) ;
return 0;
}
void GraphColor(int m)
{
int i, k;
for (i = 0; i < n; i++ ) //将数组color[n]初始化为0
color[i] = 0;
k = 0;
while (k >= 0)
{
color[k] = color[k] + 1;
while (color[k] <= m)
if (Ok(k))
break;
else
color[k] = color[k] + 1; //搜索下一个颜色
if (color[k] <= m && k == n - 1) //求解完毕,输出解
{
for (i = 0; i < n; i++)
cout<<color[i]<<" ";
return;
}
else if (color[k] <= m && k < n - 1)
k = k + 1; //处理下一个顶点
else
{
color[k] = 0;
k = k - 1; //回溯
}
}
}
int Ok(int k) //判断顶点k的着色是否发生冲突
{
for (int i = 0; i < k; i++)
if (arc[k][i] == 1 && color[i] == color[k])
return 0;
return 1;
}