๐ŸŒžAlgorithm/๐Ÿ”ฅBaekjoon

[Baekjoon] 17265_๋‚˜์˜ ์ธ์ƒ์—๋Š” ์ˆ˜ํ•™๊ณผ ํ•จ๊ป˜

๋ฟŒ์•ผ._. 2023. 5. 30. 10:37

Gold V

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

< ๋‚˜์˜ ์ธ์ƒ์—๋Š” ์ˆ˜ํ•™๊ณผ ํ•จ๊ป˜ >

 

๋ฌธ์ œ ํ’€์ด

 

bfs๋ฅผ ํ™œ์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ–ˆ๋‹ค.

 

โ“ ๋ฐฐ์—ด์„ ์™œ ์ด๋ ‡๊ฒŒ ๋งŽ์ด ์ผ๋Š”๊ฐ€?

์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์„ ์ €์žฅํ•˜๋Š” ๋ถ€๋ถ„๊ณผ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•  ๋•Œ ๊ฐ๊ฐ ๋”ฐ๋กœ ๋ฐฐ์—ด์„ ๋‘ฌ์„œ ํ™•์ธํ•˜๋Š” ๊ฒƒ์ด ํŽธํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

โ“ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’์˜ ๋ฐฐ์—ด์„ ํŠน์ • ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜์—ฌ ๋ฐฉ๋ฌธ ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๋ฐฉ๋ฌธ ํ‘œ์‹œ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•ด๋„ ๋˜์ง€ ์•Š๋Š”๊ฐ€?

์ฒ˜์Œ์—๋Š” ๊ฐ ๋ฐฐ์—ด์˜ ๊ฐ’์„ -1๋กœ ์ดˆ๊ธฐํ™”ํ•œ ํ›„ -1์ผ ๊ฒฝ์šฐ์— ๋ฌด์กฐ๊ฑด ๊ฐ’์„ ์—…๋ฐ์ดํŠธํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ๊ทธ ๊ฒฐ๊ณผ ํ‹€๋ ธ์Šต๋‹ˆ๋‹ค๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค. ๊ทธ ์ด์œ ๋Š” ์—ฐ์‚ฐํ•œ ๊ฐ’์œผ๋กœ -1์ด๋ฉฐ ๊ทธ ๊ฐ’์ด ์ตœ์†Ÿ๊ฐ’์ธ ๊ฒฝ์šฐ์—๋„ ๋ฌด์กฐ๊ฑด ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์—…๋ฐ์ดํŠธํ•ด์„œ ์ •ํ™•ํ•œ ๊ฐ’์„ ์–ป์ง€ ๋ชปํ•ด ํ‹€๋ ธ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐฐ์—ด์„ ๋”ฐ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.

 

โ“ ์—ฐ์‚ฐ์ž์ธ ๊ฒฝ์šฐ์—๋Š” ์™œ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋ฅผ ํ‘œ์‹œํ•˜์ง€ ์•Š์•˜๋Š”๊ฐ€?

๋ฐฉ๋ฌธ์—ฌ๋ถ€๋Š” ํ”ผ์—ฐ์‚ฐ์ž์ผ ๊ฒฝ์šฐ์—๋งŒ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€๋กœ ํ‘œ์‹œํ–ˆ๋‹ค. ์—ฐ์‚ฐ์ž๋ฅผ ๋ฐฉ๋ฌธ ํ‘œ์‹œ๋ฅผ ํ•  ๊ฒฝ์šฐ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๋‹ฌ๋ผ์ง€๋ฉด ์ด๋ฏธ ๋ฐฉ๋ฌธํ•œ ๊ณณ์œผ๋กœ

์—ฐ์‚ฐํ•˜์ง€ ๋ชปํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ƒ๊ธด๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

 

 

 

 my solution (Java)

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 Main { // ๋‚˜์˜ ์ธ์ƒ์—๋Š” ์ˆ˜ํ•™๊ณผ ํ•จ๊ป˜
	static char arr[][];
	static int dx[] = { 0, 1 };
	static int dy[] = { 1, 0 };

	static int max_arr[][], min_arr[][], n;
	static boolean max_visited[][], min_visited[][];

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

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

		arr = new char[n][n];
		max_arr = new int[n][n];
		min_arr = new int[n][n];
		max_visited=new boolean[n][n];
		min_visited=new boolean[n][n];

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

		bfs(0, 0, arr[0][0] + "");
		System.out.println(max_arr[n - 1][n - 1] + " " + min_arr[n - 1][n - 1]);
	}

	private static void bfs(int i, int j, String str) {
		Queue<String[]> queue = new LinkedList<>();
		queue.add(new String[] { Integer.toString(i), Integer.toString(j), str+" " });
		max_visited[i][j]=true;
		min_visited[i][j]=true;
		
		while (!queue.isEmpty()) {
			String temp[] = queue.poll();
			for (int k = 0; k < 2; k++) {
				int x = Integer.parseInt(temp[0]) + dx[k];
				int y = Integer.parseInt(temp[1]) + dy[k];
				if (x >= 0 && x < n && y >= 0 && y < n) {
					if (!Character.isDigit(arr[x][y])) { // ์—ฐ์‚ฐ์ž
						queue.add(new String[] { Integer.toString(x), Integer.toString(y), temp[2] +arr[x][y] });
					} else { // ์ˆซ์ž

						String[] num_arr=temp[2].split(" ");

						int a = Integer.parseInt(num_arr[0]);
						char op = num_arr[1].charAt(0);
						int b = arr[x][y] - '0';
						
						int result = -1;
						if (op == '+') {
							result = a + b;
						} else if (op == '-') {
							result = a - b;
						} else if (op == '*') {
							result = a * b;
						}
						
						if (!max_visited[x][y] || max_arr[x][y] < result) {
							max_visited[x][y]=true;
							max_arr[x][y] = result;
						}
						if (!min_visited[x][y] || min_arr[x][y] > result) {
							min_visited[x][y]=true;
							min_arr[x][y] = result;
						}
						
						if(max_arr[x][y]==min_arr[x][y]) {
							queue.add(new String[] { Integer.toString(x), Integer.toString(y), max_arr[x][y] + " " });
						}else {
							queue.add(new String[] { Integer.toString(x), Integer.toString(y), max_arr[x][y] + " " });
							queue.add(new String[] { Integer.toString(x), Integer.toString(y), min_arr[x][y] + " " });
						}
					}
				}
			}
		}
	}
}

 

Main

- ์ง€๋„์˜ ํฌ๊ธฐ(n) ์ž…๋ ฅ

- arr: ์ง€๋„ ์ •๋ณด ์ €์žฅ

   min_arr, max_arr : ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์˜ ์ตœ๋Œ“๊ฐ’ ์ตœ์†Ÿ๊ฐ’ ์ €์žฅ

   min_visited, max_visited: ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’ ์—ฐ์‚ฐ์˜ ๋ฐฉ๋ฌธ ์—ฌ๋ถ€ ์ €์žฅ

- bfs ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ›„ ์—ฐ์‚ฐ ๊ฒฐ๊ณผ์˜ ์ตœ๋Œ“๊ฐ’, ์ตœ์†Ÿ๊ฐ’ ์ถœ๋ ฅ

 

bfs

- queue : [x์ขŒํ‘œ, y์ขŒํ‘œ, ์ˆซ์ž+์—ฐ์‚ฐ์ž]

- queue๊ฐ€ ๋นŒ ๋•Œ๊นŒ์ง€ ๋ฐ˜๋ณต -> ์˜ค๋ฅธ์ชฝ๊ณผ ์•„๋ž˜์ชฝ์œผ๋กœ ์ด๋™ -> ๊ทธ ๊ฐ’์ด ์—ฐ์‚ฐ์ž๋ผ๋ฉด queue์— ์ €์žฅ / ์ˆซ์ž๋ผ๋ฉด ํ”ผ์—ฐ์‚ฐ์ž์™€ ์—ฐ์‚ฐ์ž ๊ตฌ๋ถ„ํ•˜์—ฌ ์—ฐ์‚ฐํ•œ ๊ฐ’ ์ €์žฅ ํ›„ ์ตœ๋Œ“๊ฐ’๊ณผ ์ตœ์†Ÿ๊ฐ’ ์—…๋ฐ์ดํŠธ ๋ฐ queue์— ์ €์žฅ

 


์ƒ๊ฐ๐Ÿค”