P1639 [USACO18FEB] Teleportation B 题解

· · 题解

P1639 [USACO18FEB] Teleportation B 题解

贪心水过

代码中的两个swap是为了保证l < r

#include <iostream>
#include <algorithm>
#include <cmath>
using namespace std;
int main(){
    int a,b,x,y; cin >> a >> b >> x >> y;
    if(x > y) swap(x,y);
    if(a > b) swap(a,b);
    cout << min(b - a,abs(x - a) + abs(y - b));//取用传送门和不用传送门的最短距离 
    return 0;
}