๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 1890_์ ํ”„

๋ฟŒ์•ผ._. 2023. 11. 30. 15:20

Silver I

๋ฌธ์ œ(์ถœ์ฒ˜: https://www.acmicpc.net/problem/1890)

< ์ ํ”„ >

 

๋ฌธ์ œ ํ’€์ด 

 

๊ฒŒ์ž„ ํŒ์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๋Š” ๋ฐฐ์—ด, ์ด๋™ ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฐ์—ด, ๊ฒฝ๋กœ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฐ์—ด ์ด 3๊ฐœ์˜ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•ด์„œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค. 

 

๋งŒ์•ฝ ๋ฌธ์ œ์—์„œ ์ฃผ์–ด์ง„ ์˜ˆ์ œ๋กœ ์„ค๋ช…ํ•ด ๋ณด์ž.

4
2 3 3 1
1 2 1 3
1 2 3 1
3 1 1 0

์™€ ๊ฐ™์ด ์˜ˆ์ œ๊ฐ€ ์ฃผ์–ด์กŒ๋‹ค๋ฉด ์ด๋™ ์œ„์น˜๋ฅผ ํ‘œ์‹œํ•˜๋Š” ๋ฐฐ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

๊ฒฝ๋กœ ์ˆ˜๋ฅผ ๊ตฌํ•˜๋Š” ๋ฐฐ์—ด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

๊ฐ ๊ฒฝ๋กœ๋Š” ๊ฐ™์€ ์ด๋™ ๋ฒˆํ˜ธ์ธ ๊ฒฝ์šฐ๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด ๋”ฐ๋กœ ๊ด€๋ฆฌํ•œ๋‹ค. ์œ„์—์„œ 4๋ฒˆ ์ด๋™ํ•˜์—ฌ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๋กœ ์˜ค๋Š” ๊ฒฝ์šฐ๊ฐ€ 2๊ฐ€์ง€์˜€๊ณ  5๋ฒˆ ์ด๋™ํ•ด์„œ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ๋„์ฐฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ 1๊ฐ€์ง€์ด๋ฏ€๋กœ ๋จผ์ € ๋„์ฐฉํ•œ ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋”ฐ๋กœ ์ €์žฅํ•ด ๋‘๊ณ  1๋กœ ์ €์žฅํ•œ๋‹ค.

 

๋ฐ˜๋ณต๋ฌธ์„ ์‚ฌ์šฉํ•ด์„œ ์ด๋™ ๋ฒˆํ˜ธ๋กœ ํƒ์ƒ‰ํ•˜๋ฉด ์•„์ง ๋ชจ๋“  ๊ฒŒ์ž„ ํŒ์„ ์ˆœํ™˜ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ์ƒˆ๋กœ์šด ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ๋  ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ ArrayList๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ’์„ ์ €์žฅํ•ด ๋’€๋‹ค๊ฐ€ ๋ชจ๋“  ํƒ์ƒ‰์ด ๋๋‚œ ํ›„ ํ•œ ๋ฒˆ์— ๊ฐ’์„ ๊ต์ฒดํ•œ๋‹ค.

 

 my solution (Java)

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

public class _1890_ { // ์ ํ”„

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

		int n = Integer.parseInt(bf.readLine());

		int arr[][] = new int[n][n];
		for (int i = 0; i < n; i++) {
			st = new StringTokenizer(bf.readLine());
			for (int j = 0; j < n; j++) {
				arr[i][j] = Integer.parseInt(st.nextToken());
			}
		}

		long move[][] = new long[n][n];
		long count[][] = new long[n][n];

		move[0][0] = 1;
		count[0][0] = 1;
		long idx = 1;
		boolean flag = false;
		long result = 0;
		ArrayList<long[]> list = new ArrayList<>();

		while (true) {
			flag = false;
			for (int i = 0; i < n; i++) {
				for (int j = 0; j < n; j++) {
					if (i == n - 1 && j == n - 1) {
						continue;
					}
					if (move[i][j] == idx) {
						if (arr[i][j] == 0) {
							continue;
						}
						flag = true;
						if (j + arr[i][j] < n) {
							list.add(new long[] { i, j + arr[i][j], count[i][j] });
						}
						if (i + arr[i][j] < n) {
							list.add(new long[] { i + arr[i][j], j, count[i][j] });
						}
					}
				}
			}
			for (long[] temp : list) {
				int a = (int) temp[0];
				int b = (int) temp[1];
				if (move[a][b] != idx + 1) {
					if (a == n - 1 && b == n - 1) {
						result += count[a][b];
					}
					count[a][b] = 0;
				}
				move[a][b] = idx + 1;
				count[a][b] += temp[2];
			}
			idx++;
			list.clear();
			if (!flag) {
				break;
			}
		}
		System.out.println(result + count[n - 1][n - 1]);
	}
}
๋ณ€์ˆ˜)
n : ๊ฒŒ์ž„ํŒ์˜ ํฌ๊ธฐ
arr : ๊ฒŒ์ž„ํŒ ์ •๋ณด
move : ์ด๋™ ์œ„์น˜ ๋ฒˆํ˜ธ ํ‘œ์‹œ
count : ๊ฒฝ์šฐ์˜ ์ˆ˜ ํ‘œ์‹œ
idx : ์ด๋™ ๋ฒˆํ˜ธ
flag : ์ด๋™ํ•  ๊ฒƒ์ด ๋‚จ์•„์žˆ๋Š”์ง€ ์—ฌ๋ถ€
result : ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜ ์นธ์œผ๋กœ ๋„์ฐฉํ•˜๋Š” ๊ฒฝ๋กœ์˜ ๊ฐœ์ˆ˜ ์ €์žฅ
list : ๊ฐ’ ๊ต์ฒด๋ฅผ ์œ„ํ•œ ArrayList

 

move ๋ฐฐ์—ด์„ ์ „์ฒด ์ˆœํ™˜ํ•˜๋ฉด์„œ ํ˜„์žฌ ์ด๋™ ๋ฒˆํ˜ธ์™€ ๊ฐ™๋‹ค๋ฉด ์˜ค๋ฅธ์ชฝ ๋˜๋Š” ์•„๋ž˜๋กœ ์ด๋™ ๊ฐ€๋Šฅํ•œ์ง€ ํŒ๋‹จ ํ›„ ์ด๋™ ๊ฐ€๋Šฅํ•˜๋‹ค๋ฉด list์— [x ์ขŒํ‘œ, y ์ขŒํ‘œ, ๊ฒฝ๋กœ ์ˆ˜ ]๋ฅผ ์ €์žฅํ•œ๋‹ค. move ๋ฐฐ์—ด ์ „์ฒด ์ˆœํ™˜์ด ๋๋‚˜๋ฉด ๊ฐ’์„ ๊ต์ฒดํ•˜๊ธฐ ์œ„ํ•ด list๋ฅผ ์ˆœํ™˜ํ•œ๋‹ค. ์ด๋•Œ, ์ด๋™ํ•  ๊ณณ์— idx+1 ๊ฐ’์ด ์•„๋‹ˆ๋ผ๋ฉด ์ด๋ฏธ ์ง€๋‚˜๊ฐ„ ๊ฒฝ๋กœ์˜ ์ˆ˜์™€ ๊ฒน์น˜์ง€ ์•Š๊ธฐ ์œ„ํ•ด ๊ฐ’์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค. ๋งŒ์•ฝ ์ด๋™ํ•  ์œ„์น˜๊ฐ€ ๊ฐ€์žฅ ์˜ค๋ฅธ์ชฝ ์•„๋ž˜๋ผ๋ฉด ์ •๋‹ต์— ํฌํ•จ์‹œ์ผœ์•ผ ํ•˜๋ฏ€๋กœ result์— ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค. move์™€ count์— ์ƒˆ๋กœ์šด ๊ฐ’์„ ์ €์žฅํ•œ๋‹ค.

๋” ์ด์ƒ ์ด๋™ํ•  ๊ณณ์ด ์—†๋‹ค๋ฉด ์ข…๋ฃŒํ•œ ํ›„์— result+count [n-1][n-1]์„ ์ถœ๋ ฅํ•œ๋‹ค.