[백준 1085] 직사각형에서 탈출 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
입력으로 주어지는 위치에서 직사각형의 경계선까지의 최소 거리를 구하는 문제입니다. 
직사각형의 경계선까지의 거리는 4가지의 경우가 있습니다. 
- 왼쪽으로 이동하는 거리 : 
x - 오른쪽으로 이동하는 거리 : 
w-x - 아래로 이동하는 거리 : 
y - 위로 이동하는 거리 : 
h-y 
위의 경우 중에서 최솟값으로 계산된 값을 선택하면 됩니다. 
Code
[ C# ] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
namespace Solution {
  class Program {
    static void Main(string[] args) {
      var input = Console.ReadLine()!.Split(' ');
      var x = int.Parse(input[0]);
      var y = int.Parse(input[1]);
      var w = int.Parse(input[2]);
      var h = int.Parse(input[3]);
      var minDist = Math.Min(Math.Min(x, y), Math.Min(w - x, h - y));
      Console.WriteLine(minDist);
    }
  }
}
[ C++ ] 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int x, y, w, h; cin >> x >> y >> w >> h;
  int minDist = min({x, y, w - x, h - y});
  cout << minDist << "\n";
  return 0;
}