20150201
题目:
一个无限长的直线海岸,要在海岸上建造雷达监察海上小岛.如何建,使用最少的雷达.
输入:
小岛数 雷达监察半径
小岛坐标x 小岛坐标y
输出:
最小雷达数
解题思路:
以小岛为中心画圆,与海岸的相交区间.
代码:
#include <iostream>
#include <cmath>
using namespace std;
#define MAX 1000
int islandX[MAX];
int islandY[MAX];
int cases = 0;
int main()
{
int numOfIsland,radium;
while(cin >> numOfIsland >> radium){
int counts = 0;
bool flag = false;
//中止条件
if(!numOfIsland&&!radium)
break;
//输入
int tempX,tempY;
for(int i = 0; i < numOfIsland; i++){
cin >> tempX >> tempY;
if(tempY>radium)
flag = true;
//插入排序
int j;
for(j = i-1; j >= 0; j--){
if(islandX[j]>tempX){
islandX[j+1] = islandX[j];
islandY[j+1] = islandY[j];
}else{
break;
}
}
islandX[j+1] = tempX;
islandY[j+1] = tempY;
}
//不可解条件
if(flag){
cout << "Case " << ++cases << ": " << -1 << endl;
continue;
}
//小岛数为0时
if(!numOfIsland){
cout << "Case " << ++cases << ": " << 0 << endl;
continue;
}
//解法 以小岛为中心画圆
counts = 1;
double left[MAX];
double right[MAX];
double temp = islandX[0] + sqrt(radium*radium-islandY[0]*islandY[0]);
for(int i = 1; i < numOfIsland; i++){
left[i] = islandX[i] - sqrt(radium*radium-islandY[i]*islandY[i]);
right[i] = islandX[i] + sqrt(radium*radium-islandY[i]*islandY[i]);
if(left[i] > temp){
temp = right[i];
counts++;
}else if(right[i] < temp){
temp = right[i];
}
}
cout << "Case " << ++cases << ": " << counts << endl;
}
return 0;
}