[백준]2931 가스관 with Java

hyeok ryu·2023년 8월 19일
0

문제풀이

목록 보기
5/154

문제

https://www.acmicpc.net/problem/2931

러시아 가스를 크로아티아로 운반하기 위해 자그레브와 모스코바는 파이프라인을 디자인하고 있다. 두 사람은 실제 디자인을 하기 전에 파이프 매니아 게임을 이용해서 설계를 해보려고 한다.

이 게임에서 유럽은 R행 C열로 나누어져 있다. 각 칸은 비어있거나, 아래 그림과 같은 일곱가지 기본 블록으로 이루어져 있다.

가스는 모스크바에서 자그레브로 흐른다. 가스는 블록을 통해 양방향으로 흐를 수 있다. '+'는 특별한 블록으로, 아래 예시처럼 두 방향 (수직, 수평)으로 흘러야 한다.

파이프 라인의 설계를 마친 후 두 사람은 잠시 저녁을 먹으러 갔다. 그 사이 해커가 침임해 블록 하나를 지웠다. 지운 블록은 빈 칸이 되어있다.

해커가 어떤 칸을 지웠고, 그 칸에는 원래 어떤 블록이 있었는지 구하는 프로그램을 작성하시오.

입력

첫째 줄에 유럽의 크기 R과 C가 주어진다. (1 ≤ R, C ≤ 25)

다음 R개 줄에는 C개 글자가 주어지며, 다음과 같은 글자로 이루어져 있다.

  • 빈칸을 나타내는 '.'
  • 블록을 나타내는 '|'(아스키 124), '-','+','1','2','3','4'
  • 모스크바의 위치를 나타내는 'M'과 자그레브를 나타내는 'Z'. 두 글자는 한 번만 주어진다.

항상 답이 존재하고, 가스의 흐름이 유일한 경우만 입력으로 주어진다, 또, 모스크바와 자그레브가 하나의 블록과 인접해 있는 입력만 주어진다. 또, 불필요한 블록이 존재하지 않는다. 즉, 없어진 블록을 추가하면, 모든 블록에 가스가 흐르게 된다.

출력

지워진 블록의 행과 열 위치를 출력하고, 어떤 블록이었는지를 출력한다.

풀이

접근방법

시간제한이 1초, 메모리가 128MB.

단순 구현이다. 경우의 수를 모두 계산해서 해결했다.

사라진 부분을 찾는 방법을 몇가지 생각해볼수 있다.
1. M에서부터 Z까지 연결된 파이프를 따라서 이동하며 빈곳을 찾기
2. 모든 비어있는 블록을 보며, 해당 위치가 빠진 블록이 맞는지 확인하기

2번 방법을 통해서 해결을 했다.
그 이유는 R과 C가 충분히 작아서 모든 비어 있는칸을 확인하더라도 여유가 충분하다고 판단하였기 때문이다.

구체적인 순서는 다음과 같다.
1. 빈 칸이라면, 주변에 연결될 수 있는 파이프가 있는지 확인해본다.
2. 연결될 수 있는 파이프가 있다면, 실제로 현재 블록과 연결 할 수 있는 블록인지, 아니면 그냥 옆을 지나가는 블록인지 확인해본다.
3. 실제 연결할 수 있는 블록이라면, 상하좌우의 블록을 참고하여 현재 블록을 결정한다.
( 단순히 상하좌우에 블록이 있다고 해서 + 모양을 넣게 된다면 아래와 같은 문제가 발생한다. )

l l l
l + l
l l l

코드

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
	static int R, C;
	static char[][] map;

	static int[] dx = {-1, 1, 0, 0};
	static int[] dy = {0, 0, -1, 1};

	static class Point {
		int x;
		int y;

		Point(int a, int b) {
			x = a;
			y = b;
		}

		public void init(int a, int b) {
			x = a;
			y = b;
		}
	}

	public static void main(String[] args) throws Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		String[] splitedLine = in.readLine().split(" ");
		R = stoi(splitedLine[0]);
		C = stoi(splitedLine[1]);

		map = new char[R][C];

		// input
		for (int i = 0; i < R; ++i)
			map[i] = in.readLine().toCharArray();

		// logic
		for (int i = 0; i < R; ++i) {
			for (int j = 0; j < C; ++j) {
				if (map[i][j] == '.')
					if (check(i, j)) {
						return;
					}
			}
		}

	}

	private static boolean check(int nextX, int nextY) {
		Point tp = new Point(nextX + dx[TOP], nextY + dy[TOP]);
		Point bp = new Point(nextX + dx[BOTTOM], nextY + dy[BOTTOM]);
		Point lp = new Point(nextX + dx[LEFT], nextY + dy[LEFT]);
		Point rp = new Point(nextX + dx[RIGHT], nextY + dy[RIGHT]);

		boolean[] chk = new boolean[4];
		if (isInRange(tp.x, tp.y) && canComeBottom(map[tp.x][tp.y])) {
			chk[0] = true;
		}
		if (isInRange(bp.x, bp.y) && canComeTop(map[bp.x][bp.y])) {
			chk[1] = true;
		}
		if (isInRange(lp.x, lp.y) && canComeRight(map[lp.x][lp.y])) {
			chk[2] = true;
		}
		if (isInRange(rp.x, rp.y) && canComeLeft(map[rp.x][rp.y])) {
			chk[3] = true;
		}

		if (chk[0] == true && chk[1] == true && chk[2] == true && chk[3] == true)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " +");
		else if (chk[0] == true && chk[1] == true && chk[2] == false && chk[3] == false)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " |");
		else if (chk[0] == true && chk[1] == false && chk[2] == true && chk[3] == false)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " 3");
		else if (chk[0] == true && chk[1] == false && chk[2] == false && chk[3] == true)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " 2");
		else if (chk[0] == false && chk[1] == true && chk[2] == true && chk[3] == false)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " 4");
		else if (chk[0] == false && chk[1] == true && chk[2] == false && chk[3] == true)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " 1");
		else if (chk[0] == false && chk[1] == false && chk[2] == true && chk[3] == true)
			System.out.println((nextX + 1) + " " + (nextY + 1) + " -");
		else {
			// nothing
			return false;
		}
		return true;
	}

	final static int TOP = 0;
	final static int BOTTOM = 1;
	final static int LEFT = 2;
	final static int RIGHT = 3;

	private static boolean isConnect(char current, char next, int d) {
		boolean result = false;
		switch (d) {
			case TOP:
				result = canGoTop(current) && canComeBottom(next);
				break;
			case BOTTOM:
				result = canGoBottom(current) && canComeBottom(next);
				break;
			case LEFT:
				result = canGoLeft(current) && canComeRight(next);
				break;
			case RIGHT:
				result = canGoRight(current) && canComeLeft(next);
				break;

		}
		return result;
	}

	private static boolean canGoTop(char c) {
		if (c == '|' || c == '+' || c == '2' || c == '3')
			return true;
		return false;
	}

	private static boolean canGoBottom(char c) {
		if (c == '|' || c == '+' || c == '1' || c == '4')
			return true;
		return false;
	}

	private static boolean canGoLeft(char c) {
		if (c == '-' || c == '+' || c == '3' || c == '4')
			return true;
		return false;
	}

	private static boolean canGoRight(char c) {
		if (c == '-' || c == '+' || c == '1' || c == '2')
			return true;
		return false;
	}

	private static boolean canComeTop(char c) {
		if (c == '|' || c == '+' || c == '2' || c == '3')
			return true;
		return false;
	}

	private static boolean canComeBottom(char c) {
		if (c == '|' || c == '+' || c == '1' || c == '4')
			return true;
		return false;
	}

	private static boolean canComeLeft(char c) {
		if (c == '-' || c == '+' || c == '3' || c == '4')
			return true;
		return false;
	}

	private static boolean canComeRight(char c) {
		if (c == '-' || c == '+' || c == '1' || c == '2')
			return true;
		return false;
	}

	private static boolean isInRange(int x, int y) {
		if (0 <= x && x < R && 0 <= y && y < C)
			return true;
		return false;
	}

	private static int stoi(String s) {
		return Integer.parseInt(s);
	}

}

0개의 댓글