文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>channel allocation(updated)

channel allocation(updated)

时间:2011-03-28  来源:专注于算法

#include <iostream>
#include <stdio.h>
#include <memory.h>
#include <cstring>
using namespace std;

int kind;
bool flag = false;
void input(int Graph[30][30], int n)
{
    char line[30];
    for (int i=1; i<=n; i++)
    {
        scanf("%s", line);
        for (int j=2; j<=n ; j++)
        {
            if (line[j] != '\0')//not '\n'
            {
                Graph[i][line[j]-'A'+1] =
                Graph[line[j]-'A'+1][i] = 1;
            }
            else
            {
                break;
            }
        }
    }
}

bool Judge(int i, int n, int colour[], int Graph[30][30])
{
    for (int j=1; j<=n; j++)
    {
        if (colour[j]==colour[i] && Graph[i][j]!=0)
        {
            return false;
        }
    }
    return true;
}
void dfs(int i, int n, int colour[], int Graph[30][30])
{
        if (i > n)
    {
                flag = true;
                return;
    }
    for (int c=1; c<=kind; c++)
    {
        colour[i] = c;
        if (Judge(i, n, colour, Graph))
        {
            dfs(i+1, n, colour, Graph);
        }
    }
}
int main()
{
    int colour[30] = {0};
    int Graph[30][30];
    memset(Graph, 0, sizeof(Graph));
    int n;
    cin >> n;
    while(n != 0)
    {
        input(Graph, n);
                for(kind=1; kind<=4; kind++)
                {
                        dfs(1, n, colour, Graph);
                        if(flag)
                        {
                                break;
                        }
                }
                if(kind == 1)
                {
                        cout << "1 channel needed." << endl;
                }
                else
                {
                        cout << kind <<" channels needed." << endl;
                }
                memset(Graph, 0, sizeof(Graph)-1);
                memset(colour, 0, sizeof(colour)-1);
                flag = false;
        cin >> n;
    }
    return 0;
}
之前发的那篇文章只能在poj上通过,却不能在joj上通过,所以改用dfs的方法。 参考资料:http://blog.sina.com.cn/s/blog_5e518b010100pkgl.html
相关阅读 更多 +
排行榜 更多 +
疯狂兔子人乐园

疯狂兔子人乐园

休闲益智 下载
空中飞机飞行

空中飞机飞行

休闲益智 下载
小姐姐历险记2

小姐姐历险记2

休闲益智 下载