AtCoder Beginner Contest 112 C - Pyramid

Problem Statement

In the Ancient Kingdom of Snuke, there was a pyramid to strengthen the authority of Takahashi, the president of AtCoder Inc.
The pyramid had center coordinates(CX,CY)and height H . The altitude of coordinates (X,Y) is max(H−|X−CX|−|Y−CY|,0).

Aoki, an explorer, conducted a survey to identify the center coordinates and height of this pyramid. As a result, he obtained the following information:

  • CX,CY was integers between 0 and 100 (inclusive), and HH was an integer not less than 1 .
  • Additionally, he obtained N pieces of information. The i-th of them is: "the altitude of point (xi,yi) is hi ."

This was enough to identify the center coordinates and the height of the pyramid. Find these values with the clues above.

Constraints

  • N is an integer between 1 and 100 (inclusive).
  • xi and yi are integers between 0 and 100 (inclusive).
  • hihi is an integer between 0 and 109 (inclusive).
  • The NN coordinates (x1,y1),(x2,y2),(x3,y3),...,(xN,yN) are all different.
  • The center coordinates and the height of the pyramid can be uniquely identified.

真的是傻,没看见max那个条件,最后改的时候又太懒,总想着在已经写好的基础上加一些条件,其实总共也没几行,重新写一下果然清晰了很多(关键是不重写就AC不了)。

最初没有看见max这个条件,所以判断的时候很显然会出问题。结束后看了题解,因为是日文的,用谷歌翻译也是个大概的意思,就看他提到全是0的情况,想到这个确实是个问题,然后开始对0加各种判断,其实这道题条件很多,每一条都按照条件直接约束就好了(噢,其实我解方程还解了一会,然后想着偷个懒遍历一下吧,果然是这样做没错了)。

思路:按照题目里的约束,中心坐标点xy都大于等于0且小于等于100,共101*101个点,并不大的范围,完全可以遍历来算,遍历每一个点,假设这个点是中心点,按照题目给出的公式,(先找一个点,不能是高度为0的点)确定金字塔的高度,然后根据目前计算出来的高和坐标计算输入中每一个坐标应有的高度,如果存在一个坐标的高度和计算出来的不一样,这个点就不是中心点,继续遍历,重复计算和验证。

#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
struct coo{
	int x;
	int y;
	int h;
}c[110];
int main()
{
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>c[i].x;
		cin>>c[i].y;
		cin>>c[i].h;
	}
	int ch;
	int p=0;
	while(c[p].h==0) p++;
	for(int i=0;i<=100;i++)
	{
		for(int j=0;j<=100;j++)
		{
			int ch=c[p].h+abs(i-c[p].x)+abs(j-c[p].y);
			int f=0;
			for(int k=0;k<n;k++)
			{
				int hh=ch-abs(i-c[k].x)-abs(j-c[k].y);
				if(hh<0) hh=0;
				if(hh!=c[k].h)
				{
					f=1;
					break;
				}
			}
			if(!f)
			{
				cout<<i<<' '<<j<<' '<<ch<<endl; return 0;
			} 
		}
	}
} 

猜你喜欢

转载自blog.csdn.net/vinacky/article/details/82954998
今日推荐