Do You Know the Way to San Jose?
题目不难,主要就是排序,不过有些细节需要注意。一个很坑的地方是 map 的两个对角点是不确定的,可能是左上和右下,也可能是左下和右上,而且两个点的顺序也不一定是左右。。。害我找了好久的bug。。。还有就是输出的时候注意单词别拼错了,空格别多打了。。因为这个WA了好几发。。
AC大概是现在唯一能让我开心的事情了。
#include<bits/stdc++.h>
using namespace std;
struct Map{
string name;
double x1,y1,x2,y2,s,rk,cx,cy,d1,ra,d2;
}m;
struct Loc{
double x,y;
}l;
vector<Map>maps;
map<string,Loc>locs;
int cmp(Map a,Map b){
return a.s > b.s;
}
int cmp2(Map a,Map b){
if(a.d1 != b.d1) return a.d1 < b.d1;
if(a.ra != b.ra) return a.ra < b.ra;
if(a.d2 != b.d2) return a.d2 < b.d2;
return a.x1 < b.x1;
}
int main(){
string s;
getline(cin,s);
while(getline(cin,s) && s != "LOCATIONS"){
stringstream ss(s);
ss>>m.name>>m.x1>>m.y1>>m.x2>>m.y2;
if(m.x1 > m.x2) swap(m.x1,m.x2); //there is a hole!!!
if(m.y1 > m.y2) swap(m.y1,m.y2);
m.cx = (m.x1 + m.x2)/2;
m.cy = (m.y1 + m.y2)/2;
m.s = (m.x2 - m.x1)*(m.y2 - m.y1);
m.ra = abs((m.x2 - m.x1)/(m.y2 - m.y1) - 0.75);
maps.push_back(m);
}
while(getline(cin,s) && s != "REQUESTS"){
stringstream ss(s);
ss>>s>>l.x>>l.y;
locs[s] = l;
}
while(getline(cin,s) && s != "END"){
int de;
stringstream ss(s);
ss>>s>>de;
cout<<s<<" at detail level "<<de<<' ';
if(!locs.count(s)){
cout<<"unknown location\n";
continue;
}
l = locs[s];
vector<Map>cans,cans2; //candidate maps
for(int i = 0;i < maps.size();i++){
m = maps[i];
if(m.x1<=l.x && m.x2>=l.x && m.y1<=l.y && m.y2 >= l.y) cans.push_back(m);
}
sort(cans.begin(),cans.end(),cmp);
if(cans.size()){
cans[0].rk = 1;
if(de == 1) cans2.push_back(cans[0]);
}
for(int i = 1;i < cans.size();i++){
if(cans[i].s < cans[i-1].s) cans[i].rk = cans[i-1].rk + 1;
else cans[i].rk = cans[i-1].rk;
if(cans[i].rk > de) break;
if(cans[i].rk == de) cans2.push_back(cans[i]);
}
if(cans2.size()){
for(int i = 0;i < cans2.size();i++){
Map t = cans2[i];
cans2[i].d1 = pow(t.cx-l.x,2) + pow(t.cy-l.y,2);
cans2[i].d2 = pow(t.x2-l.x,2) + pow(t.y2-l.y,2);
}
sort(cans2.begin(),cans2.end(),cmp2);
cout<<"using "<<cans2[0].name<<endl;
}
else{
if(!cans.size()) cout<<"no map contains that location\n";
else cout<<"no map at that detail level; using "<<cans.back().name<<endl;
}
}
return 0;
}