๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
2๏ธโƒฃ Java/Problem Solving

[Java Algorithm] ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 2 BOJ #17069

by seolhee2750 2022. 9. 30.
๋ฌธ์ œ

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

 

17069๋ฒˆ: ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 2

์œ ํ˜„์ด๊ฐ€ ์ƒˆ ์ง‘์œผ๋กœ ์ด์‚ฌํ–ˆ๋‹ค. ์ƒˆ ์ง‘์˜ ํฌ๊ธฐ๋Š” N×N์˜ ๊ฒฉ์žํŒ์œผ๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๊ณ , 1×1ํฌ๊ธฐ์˜ ์ •์‚ฌ๊ฐํ˜• ์นธ์œผ๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๋‹ค. ๊ฐ๊ฐ์˜ ์นธ์€ (r, c)๋กœ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค. ์—ฌ๊ธฐ์„œ r์€ ํ–‰์˜ ๋ฒˆํ˜ธ, c๋Š” ์—ด์˜

www.acmicpc.net

 

๋‚ด ๋ฌธ์ œ ํ’€์ด
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class Main {

	static int n;
	static int[][] map;
	static long[][][] memo; // ๊ฐ€์žฅ ํ•˜์œ„ ์ธ๋ฑ์Šค 0, 1, 2 == ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ 
	
	public static long dp() {
		for(int i = 1; i < n+1; i++) {
			for(int j = 3; j < n+1; j++) {
				if(map[i][j] == 1) continue; // ๋ฒฝ์ด๋ฉด ์–ด๋–ค ํŒŒ์ดํ”„๋„ ๋ถˆ๊ฐ€ํ•˜๋ฏ€๋กœ continue
				memo[i][j][0] = memo[i][j-1][0] + memo[i][j-1][2]; // ๊ฐ€๋กœ
				if(i == 1) continue; // ์ฒซ ๋ฒˆ์งธ ์ค„์ผ ๊ฒฝ์šฐ, ์„ธ๋กœ๊ฐ€ ์ ˆ๋Œ€ ๋ถˆ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์— continue
				memo[i][j][1] = memo[i-1][j][1] + memo[i-1][j][2]; // ์„ธ๋กœ
				if(map[i-1][j] == 1 || map[i][j-1] == 1) continue; // ๋Œ€๊ฐ์„ ์œผ๋กœ ์˜ค๋ ค๋ฉด ์œ„, ์•„๋ž˜๋„ ๋ชจ๋‘ ๋นˆ ์นธ์ด์–ด์•ผ ํ•˜๋ฏ€๋กœ continue
				memo[i][j][2] = memo[i-1][j-1][0] + memo[i-1][j-1][1] + memo[i-1][j-1][2]; // ๋Œ€๊ฐ์„ 
			}
		}
		
		return memo[n][n][0] + memo[n][n][1] + memo[n][n][2];
	}
	
	public static void main(String[] args) throws NumberFormatException, IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st;
		
		n = Integer.parseInt(in.readLine());
		map = new int[n+1][n+1];
		for(int i = 1; i < n+1; i++) {
			st = new StringTokenizer(in.readLine());
			for(int j = 1; j < n+1; j++) {
				map[i][j] = Integer.parseInt(st.nextToken());
			}
		}
		
		memo = new long[n+1][n+1][3];
		memo[1][2][0] = 1;
		if(map[n][n] == 1) {
			System.out.println(0);
		} else {
			System.out.println(dp());
		}
	}
}

๐Ÿ‘‰ DP ๋ฌธ์ œ

ํ˜„์žฌ ํŒŒ์ดํ”„์—์„œ ๋‹ค์Œ ํŒŒ์ดํ”„๋กœ ๊ฐˆ ๋•Œ ์–ด๋–ป๊ฒŒ ๊ฐˆ์ง€๋ฅผ ์ƒ๊ฐํ•˜๊ธฐ๋ณด๋‹ค,

ํ˜„์žฌ ์ž๋ฆฌ์— (๊ฐ€๋กœ or ์„ธ๋กœ or ๋Œ€๊ฐ์„ )์˜ ๋ฐฉํ–ฅ์œผ๋กœ ์˜ค๋Š”๋ฐ์—๋Š” ๋ช‡ ๊ฐœ์˜ ๊ฒฝ์šฐ๊ฐ€ ์žˆ์„์ง€๋ฅผ ์ƒ๊ฐํ•ด ์ฃผ๋Š”๊ฒŒ ํฌ์ธํŠธ โœจ

  • map๊ณผ ๊ฐ™์€ ํฌ๊ธฐ์˜ 3์ฐจ์› ๋ฐฐ์—ด memo๋ฅผ ๋งŒ๋“ค๊ณ , memo์˜ ๊ฐ€์žฅ ํ•˜์œ„๋Š” 3์˜ ํฌ๊ธฐ๋กœ ๋งŒ๋“ค์–ด์„œ
    ์ธ๋ฑ์Šค 0, 1, 2 ์ˆœ์„œ๋Œ€๋กœ ๊ฐ€๋กœ, ์„ธ๋กœ, ๋Œ€๊ฐ์„ ์œผ๋กœ ์˜ฌ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ์ €์žฅํ•˜๋„๋ก ํ–ˆ๋‹ค.
  • memo์— ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๋Š” ํŒŒ์ดํ”„์˜ ์ •๋ณด๋ฅผ ๋‹ด์•„ ์ดˆ๊ธฐํ™”ํ•ด์ฃผ๊ณ , dp ๋ฉ”์†Œ๋“œ์—์„œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ˆ„์ ํ–ˆ๋‹ค.
  • dp์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ค„์˜ ์„ธ ๋ฒˆ์งธ ์ž๋ฆฌ(์ฒซ ํŒŒ์ดํ”„์˜ ๋‹ค์Œ ์ž๋ฆฌ)๋ถ€ํ„ฐ ํƒ์ƒ‰์„ ์‹œ์ž‘ํ•ด์ฃผ์—ˆ๋Š”๋ฐ
    ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์กฐ๊ฑด์— ๋งž์ถฐ ๊ฐ๊ฐ ์„ธ๋กœ, ๊ฐ€๋กœ, ๋Œ€๊ฐ์„ ์œผ๋กœ ํ•ด๋‹น ์ž๋ฆฌ์— ๊ฐˆ ์ˆ˜ ์žˆ๋Š” ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋ˆ„์ ํ–ˆ๋‹ค.

 

๐Ÿ’ก ํ”ผ๋“œ๋ฐฑ
  • ์ฒ˜์Œ์—” ์ด ๋ฌธ์ œ๋ฅผ ์žฌ๊ท€๋กœ ํ’€์—ˆ๋Š”๋ฐ(ํŒŒ์ดํ”„ ์˜ฎ๊ธฐ๊ธฐ 1 ๋ฌธ์ œ), 3์ฐจ์› ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์„œ ๊ฐ ์ž๋ฆฌ๋งˆ๋‹ค
    ๊ฐ€๋Šฅํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๊ตฌํ•ด์ฃผ๋ฉด ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ํ’€์ดํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.
    ๐Ÿ‘‰ ์ด ๋ฌธ์ œ๊ฐ€ ์–ด๋ ค์šฐ์‹  ๋ถ„๋“ค์€ RGB ๊ฑฐ๋ฆฌ ๋ฌธ์ œ๋ฅผ ๋จผ์ € ํ’€๊ณ  ์˜ค์‹œ๋ฉด ์‰ฝ๊ฒŒ ์ดํ•ด ๊ฐ€๋Šฅํ•˜์‹ค ๋“ฏ ํ•ฉ๋‹ˆ๋‹น!

๋Œ“๊ธ€