Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C Connect Three

版权声明:欢迎转载欢迎评论! https://blog.csdn.net/rabbit_ZAR/article/details/85231941

题目:Connect Three


思路:

模拟。

可以先把ABC三点按照横坐标排序,然后在A和B之间连一条先向左后向上的路,同理在B和C之间连一条先向下后向右的路。最后统计一下。

我们假设横着的那一段距离是一定要走的,要尽可能的省下竖着的那一段距离的花费,就让两条路线竖着的部分尽可能的重合。所以我们把这一段都安排在B点那一列就好了。


代码:

#include<bits/stdc++.h>
using namespace std;

#define read(x) scanf("%d",&x)
#define maxn 1000

struct Pair{
	int x,y;
	Pair(){}
	Pair(int xx,int yy) {x=xx,y=yy;}
	bool operator < (const Pair& oth) const {
		return x<oth.x;
	}
};

Pair a[5];
bool b[maxn+5][maxn+5];

int main() {
	read(a[1].x),read(a[1].y);
	read(a[2].x),read(a[2].y);
	read(a[3].x),read(a[3].y);
	
	sort(a+1,a+4);
	
	for(int j=a[1].x;j<=a[2].x;j++) b[j][a[1].y]=true;
	for(int j=min(a[1].y,a[2].y);j<=max(a[1].y,a[2].y);j++) b[a[2].x][j]=true;
	
	for(int j=a[2].x;j<=a[3].x;j++) b[j][a[3].y]=true;
	for(int j=min(a[2].y,a[3].y);j<=max(a[2].y,a[3].y);j++) b[a[2].x][j]=true;
	
	vector<Pair> ans;
	for(int i=0;i<=1000;i++) {
		for(int j=0;j<=1000;j++) {
			if(b[i][j]) ans.push_back(Pair(i,j));
		}
	}
	
	printf("%d\n",ans.size());
	for(int i=0;i<ans.size();i++) printf("%d %d\n",ans[i].x,ans[i].y);
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/rabbit_ZAR/article/details/85231941