Need help with the easiest problem on usaco

http://www.usaco.org/index.php?page=viewproblem2&cpid=807


Am I not taking into account some cases or something?

#include <iostream>
#include <fstream>
using namespace std;

int main(){
ifstream fin("teleport.in");
ofstream fout("teleport.out");

int a;
int b;
int x;
int y;
int mindistance;


//a = b, distance = 0
//x = y, distance = abs a - b
//x is closer to a, distance = abs a - x + abs b - y
//x is closer to b, distance = abs b - x + abs a - y
fin >> a >> b >> x >> y;
if(a==b){mindistance = 0;}
if(x==y){mindistance = abs(a - b);}
if((abs(a - x)) < (abs(b - x))){mindistance = abs(a - x) + abs(b - y);}
if((abs(a - x)) > (abs(b - x))){mindistance = abs(b - x) + abs(a - y);}
if((abs(a - x)) == (abs(b - x))){mindistance = abs(a - x)*2;}
fout << mindistance;
}

First of all, I think your code is longer than it should be…

This is honestly all you need:

int a, b, x, y; cin >> a >> b >> x >> y;
cout << min(abs(b - a), min(abs(a - x) + abs(b - y),  abs(a - y) + abs(b - x))) << endl;

Also, if you want to see why your code doesn’t work, why don’t you try stress testing your code?

That problem has some cases. I wouldn’t say it is the easiest problem on USACO

How do I stress test my code?

The purpose of stress testing is to find a small test case where your solution gives a different output than the official solution. It’s linked to in How to ask for help on a problem.

But in this case, there’s no need for stress testing since the official test data is already small enough to simulate by hand.

how did u use three things in the min?

min(x, min(y, z))

This finds the min of (x, y, z)…

Another way is min({x, y, y})

Yes, you could do that too…