BOJ 12886: 돌 그룹(Java)

박철민·2023년 4월 10일
0

알고리즘 풀이

목록 보기
9/13

풀이

아이디어

BFS로 균등한 경우를 탐색하면 됩니다.
A와 B와 C의 합이 3의 배수가 아니면 균등하지 않기 때문에 0을 출력합니다.
Queue에 A, B, C를 넣습니다.

이제 다음 스텝으로
1) (A, B)를 비교하여 넣거나
2) (B, C)를 비교
3) (A, C)를 비교

다음과 같이 비교하여 작은 수를 X + X로 큰 수를 Y -X로 만들어줍니다.

특이점으로는 visit를 통해서 방문 여부를 기록할 때 이차원으로만 해도 모든 경우를 확인 할 수 있습니다.

A, B, (모든 합 - (A + B))이기 때문에 C까지 Visit를 처리할 필요가 없습니다.

상세구현

A + B + C가 3의 배수가 아니라면 균등하게 분배가 되지 않습니다.

		if((A + B + C) %3 != 0) {
			System.out.println(0);
			return;
		}

큐에 처음 값인 A,B,C를 넣어줍니다.

		queue.add(new int[] {A, B, C});
  		visit[A][B] = true;

이제 큐로 모든 갈 수 있는 경우를 탐색합니다.

만약 모든 탐색을 하였지만 A==B==C인 경우가 없다면 0을 출력합니다.

		while(!queue.isEmpty()) {
        // ------ 중략 -------- //
        }
        system.out.println(0);

이제 큐를 진행하면서

1) (A, B)를 비교
2) (B, C)를 비교
3) (A, C)를 비교

를 구현합니다.

		while(!queue.isEmpty()) {
			int[] cur = queue.poll();
			
			if(cur[0]==cur[1] && cur[1] == cur[2]) {
				System.out.println(1);
				return;
			}
			
			if(cur[0]<cur[1]) {
				int nx = cur[0] * 2;
				int ny = cur[1] - cur[1];
				
				if(!visit[nx][ny]) {
					visit[nx][ny] = true;
					queue.add(new int[] {nx, ny, cur[2]});
				}
			}
			else if(cur[0]>cur[1]) {
				int nx = cur[0] - cur[1];
				int ny = cur[1] * 2;
				
				if(!visit[nx][ny]) {
					visit[nx][ny] = true;
					queue.add(new int[] {nx, ny, cur[2]});
				}
			}
            //------중략-----//

다음과 같이 A와 B를 비교합니다.

코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;

public class No12886 {
	static int A, B, C;

	public static void main(String[] args) throws IOException {
		input();
		pro();
	}

	private static void pro() {
		if((A + B + C) %3 != 0) {
			System.out.println(0);
			return;
		}
		
		boolean [][] visit= new boolean[1501][1501];
		Queue<int[]> queue = new LinkedList<int[]>();
		queue.add(new int[] {A, B, C});
		visit[A][B] = true;
		
		while(!queue.isEmpty()) {
			int[] cur = queue.poll();
			
			if(cur[0]==cur[1] && cur[1] == cur[2]) {
				System.out.println(1);
				return;
			}
			
			if(cur[0]<cur[1]) {
				int nx = cur[0] * 2;
				int ny = cur[1] - cur[1];
				
				if(!visit[nx][ny]) {
					visit[nx][ny] = true;
					queue.add(new int[] {nx, ny, cur[2]});
				}
			}
			else if(cur[0]>cur[1]) {
				int nx = cur[0] - cur[1];
				int ny = cur[1] * 2;
				
				if(!visit[nx][ny]) {
					visit[nx][ny] = true;
					queue.add(new int[] {nx, ny, cur[2]});
				}
			}
			
			if(cur[1]<cur[2]) {
				int ny = cur[1] * 2;
				int nz = cur[2] - cur[1];
				
				if(!visit[cur[0]][ny]) {
					visit[cur[0]][ny] = true;
					queue.add(new int[] {cur[0], ny, nz});
				}
			}
			else if(cur[1]>cur[2]) {
				int ny = cur[1] - cur[2];
				int nz = cur[2] * 2;
				
				if(!visit[cur[0]][ny]) {
					visit[cur[0]][ny] = true;
					queue.add(new int[] {cur[0], ny, nz});
				}
			}
			
			if(cur[0]<cur[2]) {
				int nx = cur[0] * 2;
				int nz = cur[2] - cur[0];
				
				if(!visit[nx][cur[1]]) {
					visit[nx][cur[1]] = true;
					queue.add(new int[] {nx, cur[1], nz});
				}
			}
			else if(cur[0]>cur[2]) {
				int nx = cur[0] - cur[2];
				int nz = cur[2] * 2;
				
				if(!visit[nx][cur[1]]) {
					visit[nx][cur[1]] = true;
					queue.add(new int[] {nx, cur[1], nz});
				}
			}
		}
		System.out.println(0);
	}

	private static void input() throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = new StringTokenizer(br.readLine());

		A = Integer.parseInt(st.nextToken());
		B = Integer.parseInt(st.nextToken());
		C = Integer.parseInt(st.nextToken());
		
		br.close();
	}
}
profile
멘땅에 헤딩하는 사람

0개의 댓글