您的位置:首页 > 其它

poj 1129Channel Allocation(染色问题, DFS)

2011-08-22 15:50 344 查看
                        我表示对深搜很纠结……

#include<stdio.h>
#include<string.h>
int n;
int map[27][27];//邻接矩阵表示图
int color[27];//color[i]表示第i个节点的颜色

bool Judge(int step)//判断
{
int i;
for(i=0; i<n; i++)
{
if(map[step][i] && color[step]==color[i])
return false;
}
return true;
}
bool dfs(int col, int step)
{
int i;
if(col==4 || step>=n) return true;//得到可行解
for(i=1; i<=col; i++)
{
color[step]=i;//修改节点状态
if( Judge(step) )//如果第step个点,能染成颜色i,则扩展下一个节点
return dfs(col, step+1);
color[step]=0;//回溯节点的状态
}
return false;
}
int main()
{
char str[29];
int i, j, len;
while( scanf("%d", &n) && n)
{
memset(map, 0, sizeof(map));
for(i=0; i<n; i++)
{
scanf("%s", str);
len=strlen(str);
for(j=2; j<len; j++)
map[str[0]-'A'][str[j]-'A']=1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  allocation 扩展