| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 76845 | Accepted: 17213 |
Description
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.
Figure A Sample Input of Radar Installations
Input
The input is terminated by a line containing pair of zeros
Output
Sample Input
3 2 1 2 -3 1 2 1 1 2 0 2 0 0
Sample Output
Case 1: 2 Case 2: 1
Source
#include<iostream>
#include<list>
#include<vector>
#include<cmath>
#include<algorithm>
#include<set>
#include<fstream>
using namespace std;
struct node
{
float l;
float r;
};
bool comp(const node &a,const node &b)
{
return a.l<b.l;
}
int subsetn(vector<node> s);
bool isIntersect(node a,node b);
node calIntersect(node a,node b);
int main()
{
int n;
float d;
cin>>n>>d;
for(int i=0;n!=0||d!=0;cin>>n>>d,i++)
{
//save the data!
vector<pair<float,float> >v(n);
for(int j=0;j<n;j++)
{
cin>>v[j].first>>v[j].second;
}
//decide the condition.
bool flag = true;
if(d<0)
{
cout<<"Case "<<i+1<<": "<<-1<<endl;
flag = false;
}
else
{
for(int j=0;j<n;j++)
{
if(v[j].second>d || v[j].second<0)
{
cout<<"Case "<<i+1<<": "<<-1<<endl;
flag = false;
break;
}
}
}
//solve the problem when flag == true
if(flag == true)
{
//make the set of range
vector<node> s(n);
for(int j=0;j<n;j++)
{
s[j].l = v[j].first - sqrt(pow(d,2)-pow(v[j].second,2));
s[j].r = v[j].first + sqrt(pow(d,2)-pow(v[j].second,2));
//cout<<s[j].l<<' '<<s[j].r<<endl;
}
//work out the public subset
sort(s.begin(),s.end(),comp);
cout<<"Case "<<i+1<<": "<<subsetn(s)<<endl;
}
else
{
//do nothing!
}
}
return 0;
}
int subsetn(vector<node> s)
{
vector<set<int> > record;
vector<bool> visit(s.size(),false);
for(int i=0;i<s.size();i++)
{
set<int> stemp;
if(visit[i] == false)
{
visit[i] = true;
stemp.insert(i);
node ntemp = s[i];
for(int j=0;j<s.size();j++)
{
if(visit[j]==false && isIntersect(ntemp,s[j]))
{
visit[j] = true;
stemp.insert(j);
ntemp = calIntersect(ntemp,s[j]);
}
}
record.push_back(stemp);
}
}
return record.size();
}
bool isIntersect(node a,node b)
{
return a.r >= b.l && b.r >= a.l;
}
node calIntersect(node a,node b)
{
node result;
result.l = max(a.l,b.l);
result.r = min(a.r,b.r);
return result;
}
895




被折叠的 条评论
为什么被折叠?



