Algorithm/math
[백준] 알고리즘 C++ 1085번 - 직사각형에서 탈출
낭강
2021. 9. 30. 23:27
문제
https://www.acmicpc.net/problem/1085
1085번: 직사각형에서 탈출
한수는 지금 (x, y)에 있다. 직사각형은 각 변이 좌표축에 평행하고, 왼쪽 아래 꼭짓점은 (0, 0), 오른쪽 위 꼭짓점은 (w, h)에 있다. 직사각형의 경계선까지 가는 거리의 최솟값을 구하는 프로그램
www.acmicpc.net
소스코드
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int x,y,w,h;
cin>>x>>y>>w>>h;
int dx=min(w-x,x);
int dy=min(h-y,y);
cout<<min(dx,dy);
return 0;
}
해설
처음에 좌표 범위르 보지않고 x,y 좌표가 직사각형 밖에도 있을거라 생각했는데 문제의 조건에서의 x,y는 직사각형의 틀에서 벗어나지 않는다는 것을 알 수 있다.
그렇다면 x좌표와 y좌표 각각 직사각형 변으로 가는 최소의 거리를 구하면 된다.