1. 程式人生 > >Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4) C Connect Three

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

題目: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; }