四色问题
程序员文章站
2022-05-20 23:41:09
...
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdlib>
#include <stack>
#include <vector>
#include <queue>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 110;
int graph[MAXN][MAXN];
int color[MAXN];
int n,m,num_col;
bool Judge(int x,int col)//判断当前情况下,这个点能不能是col这个颜色
{
for(int i = 1;i <= n;i ++)
{
//纵轴是点 横纵是颜色
if(graph[i][x] && color[i] != 0 && color[i] == col )
return false;
}
return true;
}
void Init()
{
scanf("%d%d",&n,&m);//n边的条数,m点的个数
int a,b;
scanf("%d",&num_col);//能用的颜色数目
memset(graph,0,sizeof(graph));//存图
memset(color,0,sizeof(color));//每个点的颜色
for(int j =0 ;j < n;j ++)
{
scanf("%d%d",&a,&b);
graph[a][b] = graph[b][a] = 1;
}
}
void Solve(int pos)
{
if(pos == n)//解决每一条边
{
for(int i =1 ;i <= m;i ++)//每一个点的颜色
printf("%d%c",color[i],i==m?'\n':' ');
return ;
}
for(int i = 1;i <= num_col;i ++)
{
if(Judge(pos,i))
{
color[pos] = i;
Solve(pos+1);
color[pos] = 0;
}
}
}
int main()
{
freopen("in.txt","r",stdin);
Init();
Solve(1);
}
上一篇: [wp7软件]wp7~~HTC官方软件~~集合贴~~
下一篇: 3Dmax右上角小方块该怎么删除?