贪心算法解决雷达站建站问题

问题描述:

Assume the coasting is an infinite straight line. Land is in one side of coasting,
sea in the other. Each small island is a point locating in the sea side. And any
radar installation, locating on the coasting, can only cover d distance, so an
island in the sea can be covered by a radius installation, if the distance between
them is at most d.
We use Cartesian coordinate system, defining the coasting is the x-axis. The
sea side is above x-axis, and the land side below. Given the position of each
island in the sea, and given the distance of the coverage of the radar installation,
your task is to write a program to find the minimal number of radar installations
to cover all the islands. Note that the position of an island is represented by its
x-y coordinates.

解题:

        因为所有的雷达站都是在X坐标轴上,而所有的岛都是在X轴的上方。因此,若要找出雷达站的数量,首先应该弄清楚雷达站的坐标。

     (1)对于坐标为(x,y)的小岛,若要使位于x轴上的雷达站覆盖到它,则以此小岛为圆心,以雷达覆盖半径为半径画圆,此圆与x轴的交界区间即为雷达的部署区间。即为 ( x-sprt(d*d-y*y) ,x+sprt(d*d-y*y) )。设小岛i的左右区间点分别放在island[i][0],island[i][1]。

  (2)以(1)中的方法找出每个小岛如果被覆盖,雷达站安置的区间。并以所有区间的右区间点从小到大进行排列。即依据island[i][1]进行排列。

  (3)首先将island[0][1]设为第一个雷达站点,并将所有左区间点island[j][0]小于island[0][1]的小岛去掉,其右区间点island[j][1]随之在数组中去掉。

  (4)对于剩下的所有island[k][0]重复(3)的过程,直到没有小岛为止。没重复进行一次(3)的操作,说明雷达站建起一座。

pseudo-code

    i=num(island);

   while(i--)

   {

     island[i][0]=land[i].x-sprt(d*d-land[i].y*land[i].y);

     island[i][1]=land[i].x+sprt(d*d-land[i].y*land[i].y);

    }

   sort(island);//依据island[i][1]进行排序;

   for(k=0;k<num(island);k++)

   {

        for(j=k+1;j<num(island);j++)   

         {

           if(island[j][0]<island[k][1])

            delete island[j];//如果小于即改点已被雷达覆盖;

          }

         radar_count++;

         sort(island);//继续对剩下的点排序;

         找出新排序的第一个点作为新k;

    }

    return radar_count;

complexity of your algorithm:

   由于在for循环里面嵌套一个排序算法,因此时间复杂度为(n^2*logn)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

G11176593

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值