Skip to content

USACO 2016 December Contest Bronze Division - Square Pasture#

Problem link: here

Solution Author: Stefan Dascalescu

Problem Solution#

Find the maximum and the minimum among all the \(x\) and \(y\) coordinates, and the answer will be the square of whichever difference is greater as we need to make it a square.

Source codes#

The source codes in C++ and Python can be seen below.

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

int main()
{
    ifstream cin("square.in");
    ofstream cout("square.out");

    int xa, ya, xb, yb;
    int xc, yc, xd, yd;
    cin >> xa >> ya >> xb >> yb;
    cin >> xc >> yc >> xd >> yd;

    int maxx = max(xb, xd) - min(xa, xc);
    int maxy = max(yb, yd) - min(ya, yc);

    cout << max(maxx, maxy) * max(maxx, maxy);

    return 0;
}
with open("square.in", "r") as fin:
    xa, ya, xb, yb = map(int, fin.readline().split())
    xc, yc, xd, yd = map(int, fin.readline().split())
maxx = max(xb, xd) - min(xa, xc)
maxy = max(yb, yd) - min(ya, yc)
side = max(maxx, maxy)
with open("square.out", "w") as fout:
    fout.write(str(side * side))