且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

动态规划算法设施位置

更新时间:2023-11-25 23:26:52

您的问题给我写一些$ C $下经常出现类似的问题的机会的手机塔放置问题的或手机基地覆盖问题的。

Your question gave me the chance to write some code for a similar problem which often appears as cellphone tower placing problem or cellphone base coverage problem.

伪code如下:

1) Sort houses in ascending order
2) Sort facilities positions and their costs in ascending order by facilities positions
3) Let dp(i) be the minimum cost to cover i houses and lastBase(j) the last base used to cover j houses
4) Set the base case dp(0) = 0 and lastBase(0) = -1
5) For each house i:
6)   Check if previous solution is either valid or in range of this new house
7)   if it is -> grab it as solution
8)   else
9)     find a new base starting from lastBase(i) + 1 which can cover this house
10)    let it be the minimum-cost one
11)  if a base could be found -> add it to the previous solution
12)  else -> Problem cannot be solved

我建议第一次尝试它自己。

I recommend trying it out yourself first.

有关完整起见:解释,图像和C ++ code 都可以在这里

For completeness' sake: explanation, images and C++ code are available here.

反馈或错误的欢迎。