SOJ 1039

1039. Phone Home

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB

Description

When relay towers for mobile telephones communicate with the mobile phones in their area, there is always the possibility of interference. So, when assigning the transmission frequency, the FCC makes sure that nearby towers have frequencies that aren't too close. On the other hand, the FCC does not want to assign too many different frequencies; they want to save as many as possible for other uses. Your job is to find an optimal assignment of frequencies.

In this problem, the frequencies will be integers. Nearby towers must be assigned frequencies that differ by at least 2. You'll find an assignment using as few frequencies as possible. For example, consider the following two arrangements of towers. Two towers near each other are indicated by the connecting line.

 

Note that the following are legal frequency assignments to these two tower configurations. However, the second arrangement does not use the fewest number of frequencies possible, since the tower with frequency 5 could have frequency 1.

Input

There will be multiple test cases. Input for each test case will consist of two lines: the first line will contain the integer n, indicating the number of towers. The next line will be of the form x1 y1 x2 y2 ... xn yn where xi yi are the coordinates of tower i. A pair of towers are considered "near" each other if the distance between them is no more than 20. There will be no more than 12 towers and no tower will have more than 4 towers near it. A value of n = 0 indicates end of input.

Output

For each test case, you should print one line in the format:

The towers in case n can be covered in f frequencies.

where you determine the value for f. The case numbers, n, will start at 1.

Sample Input

5
0 0 5 7.5 1 -3 10.75 -20.1 12.01 -22
6
0 1 19 0 38 1 38 21 19 22 0 21
0

Sample Output

The towers in case 1 can be covered in 3 frequencies.
The towers in case 2 can be covered in 2 frequencies
 
  总体思路还是用贪婪算法,具体准则就是每当为一个信号塔分配频率时,都从第一个频率开始分配,如果第一个不可行再换第二个,直到可行为止。依照这一准则最后使用的频率总数是最少的。
 
  
// Problem#: 1039
// Submission#: 5060887
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include<iostream>
#include<cmath>
#include<cstring>
using namespace std;
#define maxn 13
struct node{//节点分别存储信号塔的x坐标,y坐标和频率 
    double x;
    double y;
    int fre;
};

int LINK[maxn][maxn];//邻接矩阵,若i,j之间的距离小于20则相邻,把LINK[i][j]置为1 
node tower[maxn];//存储每个塔节点 
int n;//塔的数量 
int selected[12]={1,3,5,7,9,11,13,15,17,19,21,23};//由于塔最多有12个,所以最多有12个不同的频率,将其放在一个数组中备用 
int used[12];//用来记录某个频率是否被使用,是则置为1 
double getDis(node A,node B)//获取两塔之间的距离 
{
    return pow(A.x-B.x,2)+pow(A.y-B.y,2);
} 
void makeGra()//建图 
{
    memset(LINK,0,sizeof(LINK));
    memset(used,0,sizeof(used));
    for(int i=1;i<=n;++i)
    {
        cin>>tower[i].x>>tower[i].y;
        tower[i].fre=0;//初始频率全部设为0 
    }
    for(int i=1;i<n;++i)
    {
        for(int j=i+1;j<=n;++j)
        {
            if(getDis(tower[i],tower[j])<=400)
                LINK[i][j]=LINK[j][i]=1;
        }
    }
}

int findAns()//开始分配频率 
{
    bool flag;
    for(int i=1;i<=n;++i)
    {
        for(int j=0;j<12;++j)//贪婪准则,用来尽量减少频率数,每次分配都从第一个频率开始 
        {
            flag=1;
            for(int k=1;k<=n;++k)
            {
                if(LINK[i][k]&&tower[k].fre==selected[j])//若有相邻的信号塔且频率相同,则说明第j个频率分配给第i个塔不可行,所以跳出循环换第j+1个频率 
                {
                    flag=0;
                    break;
                }
            }
            if(flag)//flag为1则说明与所有相邻的塔都不冲突,所有第i个塔可以用第j个频率,然后跳出循环开始为第i+1个塔分配 
            {
                tower[i].fre=selected[j];
                used[j]=1;
                break;
            }
        }
    }
    int pos=0;
    for(;pos<12;++pos)//前pos个频率均被使用,所以最少使用pos个频率 
    {
        if(!used[pos])
            break;
    }
    return pos;
}

int main()
{
    int count=1;
    while(cin>>n&&n)
    {
        makeGra();
        cout<<"The towers in case "<<count<<" can be covered in "<<findAns()<<" frequencies."<<endl;
        count++;
    }
}                                 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值