본문 바로가기
Algorithm/Baekjoon

Baekjoon 24789 Railroad JAVA

by Hunveloper 2023. 10. 9.
728x90

 

24789번: Railroad

Theta likes to play with her DUPLO railway set. The railway set she has consists of pieces of straight tracks, curved tracks, Y-shaped switches, and X-shaped level junctions, as well as bridges that allow one track to cross over another.  There are also s

www.acmicpc.net

문제

Theta likes to play with her DUPLO railway set. The railway set she has consists of pieces of straight tracks, curved tracks, Y-shaped switches, and X-shaped level junctions, as well as bridges that allow one track to cross over another.  There are also straight tracks that are railroad crossings to allow car traffic to cross.

Close-ups of some of the pieces are shown below:

To play, she picks a number of X-shaped level junctions and a number of Y-shaped switches and connects them with straight and curved pieces, using bridges as necessary.

Because the set doesn't include any bumpers, she wants to build a closed track, like all the examples shown in the manual that came with the set:

Figure 1: Various track layouts possible with the DUPLO railway system. Curved pieces are shown in green, straights in red, switches in orange, level junctions in yellow, bridges in blue, and crossings in black. DUPLO is a trademark of the LEGO Group.

Unfortunately, sometimes, this doesn't seem to work with the number of X-shaped level junctions and Y-shaped switches she starts out with.

She quickly figures out exactly when it is possible to build a closed track - can you figure it out, too?

Write a program that outputs if it is possible to build a railroad that does not require any bumpers (i.e., which does not have any dead-end tracks).

입력

The input consists of a single test case with two integer numbers  () and  () denoting the number of level junctions and switches, respectively. You may assume that Theta has sufficiently many straight and curved pieces as well as bridges.

출력

Output possible if she can build a closed track using all level junctions and all switches without any dead ends, or impossible otherwise.

풀이

문제를 간단히 요약하면 모든 레일이 하나로 연결되어야 한다는 것이다.

이는 연결부 부분이 짝수가 된다면 만족하게 된다.

X형태는 갯수에 관계없이 항상 4*n의 갯수로 증가하지만,

Y형태는 3*n의 갯수로 증가하기에 Y가 2의 배수로 들어온다면 폐쇄형 선로를 만들 수 있다.

코드
import java.util.*;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int x=sc.nextInt(), y=sc.nextInt();
		System.out.println(y%2==0?"possible":"impossible");
	}
}

 

728x90
728x90

'Algorithm > Baekjoon' 카테고리의 다른 글

Baekjoon 16234 인구 이동 JAVA  (0) 2023.10.09
Baekjoon 1476 날짜 계산 JAVA  (1) 2023.10.09
Baekjoon 1049 기타줄 JAVA  (0) 2023.10.09
Baekjoon 2523 별 찍기 - 13 JAVA  (0) 2023.06.25
Baekjoon 24900 한별 찍기 JAVA  (0) 2023.06.25

댓글