本文主要是介绍Urban Elevations(UBA-211),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
网址如下:
Urban Elevations - UVA 221 - Virtual Judge (vjudge.net)
第三方网站的
说实话,我看英语看得头大
最近学了一堆stl的容器,比如map,set啥的,方便是很方便,但是导致我脑子里第一个念头就是用他,思维有点局限了
我的代码如下:
#include<vector>
#include<cstdio>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
struct Building{double x1, x2, y, h;
};int main(void)
{int kase = 0, n;while(scanf("%d", &n) == 1 && n){if(kase) putchar('\n');printf("For map #%d, the visible buildings are numbered as follows:\n", ++kase);//输入vector<Building> buildings;set<double> x_list;buildings.push_back(Building());for(int i = 0; i < n; i++){double x, y, w, d, h;scanf("%lf%lf%lf%lf%lf", &x, &y, &w, &d, &h);Building tmp{x, x + w, y, h};buildings.push_back(tmp), x_list.insert(tmp.x1), x_list.insert(tmp.x2);}//离散化xmap<double, vector<int>> xToBd;for(auto it = x_list.begin(); true; ){double mid = *it;if(++it == x_list.end()) break;mid = (mid + *it) / 2.0;xToBd[mid] = vector<int>();}//开始按y排序各离散化的x点中的建筑for(auto it = xToBd.begin(); it != xToBd.end(); it++){for(int i = 1; i <= n; i++)if(buildings[i].x1 <= it->first && it->first <= buildings[i].x2){bool is_pushback = true;for(auto it2 = it->second.begin(); it2 != it->second.end(); it2++){int ID1 = i, ID2 = *it2;if(buildings[ID1].y < buildings[ID2].y){it->second.insert(it2, ID1), is_pushback = false; break;}}if(is_pushback) it->second.push_back(i);}}//总和结果vector<int> result;bool * is_judge = new bool [n + 1]();for(auto it = xToBd.begin(); it != xToBd.end(); it++){double max_h = 0.0;for(int i = 0; i < it->second.size(); i++){int ID = it->second[i];if(max_h < buildings[ID].h){max_h = buildings[ID].h;if(!is_judge[ID]) result.push_back(ID), is_judge[ID] = true;}}}delete [] is_judge;//输出for(auto it = result.begin(); it != result.end(); it++){if(it != result.begin()) putchar(' '); printf("%d", *it);}putchar('\n');}return 0;
}
放眼望去,一堆模板
而且没过
算法书的示例代码如下:
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 100 + 5;
struct Building{int id;double x, y, w, d, h;bool operator<(const Building &rhs) const{return x < rhs.x || (x == rhs.x && y < rhs.y);}
}b[maxn];
int n;
double x[maxn * 2];
bool cover(int i, double mx){return b[i].x <= mx && b[i].x + b[i].w >= mx;
}//判断建筑i在x = mx处是否可见
bool visible(int i, double mx){if(!cover(i, mx)) return false;for(int k = 0; k < n; k++)if(b[k].y < b[i].y && b[k].h >= b[i].h && cover(k, mx)) return false;return true;
}int main(void)
{int kase = 0;while(scanf("%d", &n) == 1 && n){for(int i = 0; i < n; i++){scanf("%lf%lf%lf%lf%lf", &b[i].x, &b[i].y, &b[i].w, &b[i].d, &b[i].h);x[i * 2] = b[i].x, x[i * 2 + 1] = b[i].x +b[i].w;b[i].id = i + 1;}sort(b, b + n);sort(x, x + n * 2);int m = unique(x, x + n * 2) - x;//去重,得到m个坐标if(kase++) putchar('\n');printf("For map #%d, the visible buildings are numbered as follows:\n%d", kase, b[0].id);for(int i = 1; i < n; i++){bool vis = false;for(int j = 0; j < m - 1; j++)if(visible(i, (x[j] + x[j + 1]) / 2)){vis = true; break;}if(vis) printf(" %d", b[i].id);}putchar('\n');}return 0;
}
emmmm
倒是一个容器都没用
裂了
这篇关于Urban Elevations(UBA-211)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!