๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ”Ž Algorithm/๐Ÿ’ป ๋ฌธ์ œ ํ’€์ด

[๋ฐฑ์ค€] 1987. ์•ŒํŒŒ๋ฒณ

by Seongpyo Hong 2021. 2. 19.


ํ’€์ด

๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ DFS๋กœ ํƒ์ƒ‰ํ•˜๋ฉฐ ์ด์ „์— ๋ฐฉ๋ฌธํ•œ ์•ŒํŒŒ๋ฒณ์ธ ๊ฒฝ์šฐ ๊ฐ€์ง€์น˜๊ธฐ๋ฅผ ์ง„ํ–‰ํ•˜์—ฟ๋‹ค. ์ด ๋•Œ ์ด์ „ ๋ฐฉ๋ฌธ ๋…ธ๋“œ๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ์ดˆ๊ธฐ์—๋Š” ArrayList๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜์˜€๊ณ  ์ด์— ๋Œ€ํ•œ ์ด์œ ๊ฐ€ ArrayList์˜ remove ๊ณผ์ •์—์„œ O(N)์˜ ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋ผ๊ณ  ํŒ๋‹จํ•˜์˜€๋‹ค. ๊ทธ๋ž˜์„œ ์ด์ „ ๋ฐฉ๋ฌธ ์•ŒํŒŒ๋ฒณ์„ ์œ ์ง€ํ•˜๋Š” ๋ฐฉ์‹์„ StringBuilder๋ฅผ ์‚ฌ์šฉํ•˜์˜€๊ณ  ๋งˆ์ง€๋ง‰ ์ธ๋ฑ์Šค๋ฅผ ์‚ญ์ œํ•จ์œผ๋กœ์จ ์‹œ๊ฐ„ ์ดˆ๊ณผ๋ฅผ ํ•ด๊ฒฐํ•˜์˜€๋‹ค.

ํ•˜์ง€๋งŒ ์ด ๋ฐฉ๋ฒ•๋„ ๋„ˆ๋ฌด ๋Š๋ ค์„œ ์ด๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ ๋น„ํŠธ๋งˆ์Šคํ‚น์„ ์‚ฌ์šฉํ•˜์˜€๊ณ  ๋ชจ๋“  ๊ฒฝ์šฐ์— distance๋ฅผ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์œ ๋งํ•˜์ง€ ์•Š์€ ๊ฒฝ์šฐ์—๋งŒ distance๋ฅผ ๋น„๊ตํ•˜๋„๋ก ๊ฐœ์„ ํ•˜์˜€๋‹ค. ๋น„ํŠธ๋งˆ์Šคํ‚น์œผ๋กœ๋Š” ํ˜„์žฌ ์•ŒํŒŒ๋ฒณ - 'A'๋ฅผ ํ•ด์คŒ์œผ๋กœ์จ ํฌํ•จ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜์˜€๋‹ค.

ArrayList

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class Solution1987 {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int R, C, maxDistance = Integer.MIN_VALUE;
	static String[][] map;
	static boolean[][] check;
	static ArrayList<String> previous;
	static int[] dx = { 0, 1, 0, -1 };
	static int[] dy = { 1, 0, -1, 0 };

	public static void main(String[] args) throws IOException {
		String[] input = br.readLine().split(" ");
		R = Integer.parseInt(input[0]);
		C = Integer.parseInt(input[1]);
		map = new String[R][C];
		check = new boolean[R][C];

		for (int i = 0; i < R; i++) {
			char[] line = br.readLine().toCharArray();
			for (int j = 0; j < C; j++) {
				map[i][j] = String.valueOf(line[j]);
			}
		}

		previous = new ArrayList<String>();
		traversal(0, 0, 1);
		System.out.println(maxDistance);
	}

	private static void traversal(int x, int y, int distance) {
		maxDistance = Math.max(maxDistance, distance);
		check[x][y] = true;
		previous.add(map[x][y]);
		for (int i = 0; i < 4; i++) {
			int nextX = x + dx[i], nextY = y + dy[i];
			if (!isValid(nextX, nextY))
				continue;
			traversal(nextX, nextY, distance + 1);
		}
		
		previous.remove(map[x][y]);
		check[x][y] = false;
	}

	static boolean isValid(int x, int y) {
		if (x < 0 || y < 0 || x >= R || y >= C || previous.contains(map[x][y]) || check[x][y])
			return false;
		return true;
	}
}

StringBuilder

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

public class Main {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int R, C, maxDistance = Integer.MIN_VALUE;
	static String[][] map;
	static boolean[][] check;
	static StringBuilder previous;
	static int[] dx = { 0, 1, 0, -1 };
	static int[] dy = { 1, 0, -1, 0 };

	public static void main(String[] args) throws IOException {
		String[] input = br.readLine().split(" ");
		R = Integer.parseInt(input[0]);
		C = Integer.parseInt(input[1]);
		map = new String[R][C];
		check = new boolean[R][C];

		for (int i = 0; i < R; i++) {
			char[] line = br.readLine().toCharArray();
			for (int j = 0; j < C; j++) {
				map[i][j] = String.valueOf(line[j]);
			}
		}

		previous = new StringBuilder(String.valueOf(map[0][0]));
		traversal(0, 0, 1);
		System.out.println(maxDistance);
	}

	private static void traversal(int x, int y, int distance) {
		maxDistance = Math.max(maxDistance, distance);

		for (int i = 0; i < 4; i++) {
			int nextX = x + dx[i], nextY = y + dy[i];
			if (!isValid(nextX, nextY))
				continue;
			check[nextX][nextY] = true;
			previous.append(map[nextX][nextY]);
			traversal(nextX, nextY, distance + 1);
			check[nextX][nextY] = false;
			previous = previous.deleteCharAt(previous.length()-1);
		}
	}

	static boolean isValid(int x, int y) {
		if (x < 0 || y < 0 || x >= R || y >= C || previous.toString().contains(map[x][y])
				|| check[x][y])
			return false;
		return true;
	}
}

Bitmasking

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

public class Solution {
	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static int R, C, maxDistance = Integer.MIN_VALUE;
	static char[][] map;
	static int[] dx = { 0, 1, 0, -1 };
	static int[] dy = { 1, 0, -1, 0 };

	public static void main(String[] args) throws IOException {
		String[] input = br.readLine().split(" ");
		R = Integer.parseInt(input[0]);
		C = Integer.parseInt(input[1]);
		map = new char[R][C];

		for (int i = 0; i < R; i++) {
			char[] line = br.readLine().toCharArray();
			for (int j = 0; j < C; j++) {
				map[i][j] = line[j];
			}
		}
		int start = map[0][0] - 'A';
		traversal(0, 0, 1, 0|1<<start);
		System.out.println(maxDistance);
	}

	private static void traversal(int x, int y, int distance, int flag) {
		for (int i = 0; i < 4; i++) {
			int nextX = x + dx[i], nextY = y + dy[i];
			if (!isValid(nextX, nextY, flag)) {
				maxDistance = Math.max(maxDistance, distance);
				continue;
			}
			int bit = map[nextX][nextY] - 'A';
			traversal(nextX, nextY, distance + 1, flag|1<<bit);
		}
	}

	static boolean isValid(int x, int y, int flag) {
		if (x < 0 || y < 0 || x >= R || y >= C )
			return false;
		
		int bit = map[x][y] - 'A';
		if ((flag&1<<bit) != 0) 
			return false;
		return true;
	}
}

๋Œ“๊ธ€