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
#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
相关文章推荐
- poj 1129 Channel Allocation (DFS 图的染色问题)
- uva10004 Bicoloring 黑白染色问题,DFS
- uva10004 Bicoloring 黑白染色问题,DFS
- DFS染色解决区域分块问题UVALive 6663
- 【CodeForces 149D】 【dp+dfs好题】D. Coloring Brackets【在限制条件下括号染色问题】
- CodeForces 149D 括号染色问题 dp+dfs好题
- CF - 782C. Andryusha and Colored Balloons - DFS染色问题
- poj1321--棋盘问题(DFS)
- leetcode -- Clone Graph -- deep copy问题,dfs,bfs
- poj1321 棋盘问题 dfs入门 TWT Tokyo Olympic 1combo-1
- cf804C(dfs染色)
- poj 1419 Graph Coloring uva 193 - Graph Coloring (图着色问题,dfs)
- 区间染色问题
- POJ 1321 棋盘问题 (DFS)
- 部分和问题(南阳oj1058)(DFS)
- 棋盘问题 POJ - 1321(DFS)
- WIKI 【1295 N皇后问题 】DFS
- 【POJ】1321-棋盘问题 n皇后、dfs
- hdu 1045 zoj1002 八皇后问题 dfs
- 八皇后问题 DFS,回溯剪枝 //http://poj.grids.cn/practice/2698