์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ/BOJ_Java

[BOJ/Step9] 1085 : ์ง์‚ฌ๊ฐํ˜•์—์„œ ํƒˆ์ถœ (JAVA)

NaNaRin๐Ÿ™ƒ 2021. 1. 19. 20:54

www.acmicpc.net/problem/1085

 

1085๋ฒˆ: ์ง์‚ฌ๊ฐํ˜•์—์„œ ํƒˆ์ถœ

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

www.acmicpc.net


๋ฌธ์ œ

ํ•œ์ˆ˜๋Š” ์ง€๊ธˆ (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๋Š” ์ •์ˆ˜

 

์˜ˆ์ œ ์ž…๋ ฅ

6 2 10 3

 

์˜ˆ์ œ ์ถœ๋ ฅ

1


ํ’€์ด

1. ํ•œ์ˆ˜์˜ ์œ„์น˜๋กœ๋ถ€ํ„ฐ ๊ฐ€์žฅ ๊ฒฝ๊ณ„์„ ๊นŒ์ง€ ๊ฐ€๊นŒ์šด ๊ฑฐ๋ฆฌ๋Š” x, w-x, y, h-y ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ์ˆ˜ ์ด๋‹ค.

=> Math.min() ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ

 

import java.util.Scanner;

public class B1085 {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		
		int x = sc.nextInt();
		int y = sc.nextInt();
		int w = sc.nextInt();
		int h = sc.nextInt();
		
		int minW = Math.min(x , w - x);
		int minH = Math.min(y , h - y);
 
		System.out.println(Math.min(minW, minH));
	}
}