๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœจ Algorithm

[๋ฐฑ์ค€/C++] 1085๋ฒˆ : ์ง์‚ฌ๊ฐํ˜•์—์„œ ํƒˆ์ถœ

by nitronium102 2021. 8. 12.

๋ฌธ์ œ

ํ•œ์ˆ˜๋Š” ์ง€๊ธˆ (x, y)์— ์žˆ๋‹ค. ์ง์‚ฌ๊ฐํ˜•์€ ๊ฐ ๋ณ€์ด ์ขŒํ‘œ์ถ•์— ํ‰ํ–‰ํ•˜๊ณ , ์™ผ์ชฝ ์•„๋ž˜ ๊ผญ์ง“์ ์€ (0, 0), ์˜ค๋ฅธ์ชฝ ์œ„ ๊ผญ์ง“์ ์€ (w, h)์— ์žˆ๋‹ค. ์ง์‚ฌ๊ฐํ˜•์˜ ๊ฒฝ๊ณ„์„ ๊นŒ์ง€ ๊ฐ€๋Š” ๊ฑฐ๋ฆฌ์˜ ์ตœ์†Ÿ๊ฐ’์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— x, y, w, h๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ œ์˜ ์ •๋‹ต์„ ์ถœ๋ ฅํ•œ๋‹ค.

์ œํ•œ

  • 1 ≤ w, h ≤ 1,000
  • 1 ≤ x ≤ w-1
  • 1 ≤ y ≤ h-1
  • x, y, w, h๋Š” ์ •์ˆ˜

ํ’€์ด

minํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๋น ๋ฅด๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ์ด๋‹ค(algorithm ํ—ค๋”์— ์žˆ๋‹ค)

// ์ง์‚ฌ๊ฐํ˜•์—์„œ ํƒˆ์ถœ
#include <iostream>
#include <algorithm> // min
using namespace std;

int solution(int x, int y, int w, int h){
  int vertical = min(x, w-x);
  int horizontal = min(y, h-y);

  return min(vertical, horizontal);
}

int main() {
  int x, y, w, h;
  cin >> x >> y >> w >> h;
  cout << solution(x, y, w, h);
}

 

๋Œ“๊ธ€