J

[JAVA] BOJ ๋ฐฑ์ค€ 17825 ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด ๋ณธ๋ฌธ

๐Ÿ”‘ Problem Solving/๐Ÿ’ป Samsung SW Expert

[JAVA] BOJ ๋ฐฑ์ค€ 17825 ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด

snowball๐Ÿฐ 2023. 3. 6. 17:50

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

 

17825๋ฒˆ: ์ฃผ์‚ฌ์œ„ ์œท๋†€์ด

์ฒซ์งธ ์ค„์— ์ฃผ์‚ฌ์œ„์—์„œ ๋‚˜์˜ฌ ์ˆ˜ 10๊ฐœ๊ฐ€ ์ˆœ์„œ๋Œ€๋กœ ์ฃผ์–ด์ง„๋‹ค.

www.acmicpc.net

ํ’€์ด ๋ฐฉ๋ฒ•

  1. ๋ฏธ๋ฆฌ ์ฃผ์‚ฌ์œ„๊ฐ€ ๋„์ฐฉํ•˜๋Š” ์นธ์˜ ์ ์ˆ˜๋“ค์„ ์ €์žฅํ•ด์ค€๋‹ค. (์ด๋•Œ ํ•˜๋‚˜ ์ด์ƒ์ด ๋‹ฟ์„ ์ˆ˜ ์žˆ๋Š” ๊ณณ์ด๋ฉด ๋”ฐ๋กœ ๋ฐฐ์—ด์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ๊ฒƒ์ด ์ข‹๋‹ค.)
  2. ์ค‘๋ณต ์ˆœ์—ด์„ ์ด์šฉํ•˜์—ฌ ์ด๋™ํ•  ๋ง์„ ์ •ํ•˜๊ณ  ์ด๋™ ๊ตฌ์—ญ์— ๋ง์ด ์žˆ๋Š” ์ง€ ์กฐ์‚ฌํ•œ ๋’ค ์ด๋™ํ•œ๋‹ค.

์ „์ฒด ์ฝ”๋“œ

import java.util.*;
import java.io.*;

public class Main {
	static int[] dice;
	static int[][] hores;
	static int result;
	static int[][] map = new int[5][];
	static int[] dx = {-1, 0, 0, 1};
	static int[] dy = {0, -1, 1, 0};
	public static void main(String[] args) throws Exception {
	   BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
	   dice = new int[10];
	   hores = new int[4][2];
	   map[0] = new int[] {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
	   map[1] = new int[] {0, 13, 16, 19};
	   map[2] = new int[] {0, 22, 24};
	   map[3] = new int[] {0, 28, 27, 26};
	   map[4] = new int[] {0, 25, 30, 35};
	   StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
	   for(int i=0; i<10; i++)
		   dice[i] = Integer.parseInt(st.nextToken());
	   move(0, 0);
	   System.out.println(result);
	}
	static void move(int cnt, int sum) {
		if(cnt == 10) {
			if(result < sum) result = sum;
			return;
		}
		c: for(int i=0; i<4; i++) { // ์œ„์น˜ ์ธ๋ฑ์Šค, ๋งต ์„ ํƒ ์ธ๋ฑ์Šค
			if(hores[i][0] == 'e') continue;
			int x = hores[i][0]; int m = hores[i][1];
			if(hores[i][1] == 0 && hores[i][0] % 5 == 0 && hores[i][0] < 20) {
				hores[i][1] = hores[i][0] / 5; hores[i][0] = 0;
			}
			hores[i][0] += dice[cnt];
			if(hores[i][1] == 0 && hores[i][0] >= map[0].length)
				hores[i][0] = 'e';
			if(hores[i][1] > 0 && hores[i][1] < 4 && hores[i][0] >= map[hores[i][1]].length) {
				hores[i][0] = hores[i][0] - map[hores[i][1]].length + 1;
				hores[i][1] = 4;
			}
			if(hores[i][1] == 4 && hores[i][0] >= map[hores[i][1]].length) {
				hores[i][0] = hores[i][0] - map[hores[i][1]].length;
				hores[i][1] = 0;
				if(hores[i][0] > 0) hores[i][0] = 'e';
				else hores[i][0] = 20;
			}
			if(hores[i][0] != 'e') {
				for(int k=0; k<4; k++)
					if(k == i) continue;
					else if(hores[k][0] == hores[i][0] && hores[k][1] == hores[i][1]) {
						hores[i][0] = x; hores[i][1] = m;
						continue c;
				}
			}
			if(hores[i][0] == 'e')
				move(cnt+1, sum);
			else
				move(cnt+1, sum+map[hores[i][1]][hores[i][0]]);
			hores[i][0] = x; hores[i][1] = m;
		}
	}
}

ํ•œ์ค„์”ฉ ์‚ดํŽด๋ณด์ž

static int[] dice;
static int[][] hores;
static int result;
static int[][] map = new int[5][];

dice์—๋Š” ์ฃผ์‚ฌ์œ„ ์ •๋ณด 10๊ฐœ๋ฅผ ๋‹ด์•„์ค„ ๊ฒƒ์ด๋‹ค.

hores๋Š” ๋ง์˜ ์ •๋ณด์ด๋‹ค. index 0์—๋Š” ๋ง์˜ ์œ„์น˜, index 1์—๋Š” ๋ง์ด ์–ด๋–ค map์— ์žˆ๋Š”์ง€๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค.

map์— ๋ฏธ๋ฆฌ ๋ณด๋“œ์˜ ์ ์ˆ˜๋ฅผ ์ €์žฅํ•ด์ค€๋‹ค.

dice = new int[10];
 hores = new int[4][2];
 map[0] = new int[] {0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40};
 map[1] = new int[] {0, 13, 16, 19};
 map[2] = new int[] {0, 22, 24};
 map[3] = new int[] {0, 28, 27, 26};
 map[4] = new int[] {0, 25, 30, 35};
 StringTokenizer st = new StringTokenizer(bf.readLine(), " ");
 for(int i=0; i<10; i++)
   dice[i] = Integer.parseInt(st.nextToken());

๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด map์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์˜€๋‹ค.

static void move(int cnt, int sum) {
		if(cnt == 10) {
			if(result < sum) result = sum;
			return;
		}

move()๋ฅผ ํ†ตํ•ด ์ค‘๋ณต ์ˆœ์—ด์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ visited๋Š” ํ•„์š”์—†๋‹ค. ์ด๋–„ ํŒŒ๋ผ๋ฏธํ„ฐ cnt๋Š” ๋ช‡๋ฒˆ์งธ ์ฃผ์‚ฌ์œ„๋ฅผ ๊ตด๋ฆฐ ๊ฒƒ์ธ์ง€, sum์€ ์—ฌํƒœ๊นŒ์ง€ ์ ์ˆ˜์˜ ํ•ฉ์„ ์ €์žฅํ•˜๊ณ  ์žˆ๋‹ค.

๊ธฐ์ €์กฐ๊ฑด์— ์˜ํ•ด 10๋ฒˆ์„ ๋‹ค ๊ตด๋ ธ์œผ๋ฉด result์— sum์„ ๊ฐฑ์‹ ํ•ด์ค€๋‹ค. ๊ทธ๋ฆฌ๊ณ  return

c: for(int i=0; i<4; i++) { // ์œ„์น˜ ์ธ๋ฑ์Šค, ๋งต ์„ ํƒ ์ธ๋ฑ์Šค
			if(hores[i][0] == 'e') continue;
			int x = hores[i][0]; int m = hores[i][1];
			if(hores[i][1] == 0 && hores[i][0] % 5 == 0 && hores[i][0] < 20) {
				hores[i][1] = hores[i][0] / 5; hores[i][0] = 0;
			}

for๋ฌธ์œผ๋กœ ์–ด๋–ค ๋ง์— ์ด๋ฒˆ ์ฃผ์‚ฌ์œ„๋ฅผ ์ ์šฉํ• ์ง€ ๊ณ ๋ฅธ๋‹ค. ์ด๋ฏธ ๋ง์ด ๋์— ๋‹ฟ์•˜๋‹ค๋ฉด ๋” ์ด์ƒ ์ด๋™ํ• ์ˆ˜ ์—†์œผ๋ฏ€๋กœ continue

x์™€ m์— ํ˜„์žฌ ๋ง์˜ ์ •๋ณด๋“ค์„ ์ €์žฅํ•ด๋†“๋Š”๋‹ค.

์ฒซ๋ฒˆ์งธ map์—์„œ 10, 20, 30์˜ index๋Š” ๊ฐ๊ฐ 5, 10, 15๋กœ 5์˜ ๋ฐฐ์ˆ˜์ด๋‹ค. ์ด ์œ„์น˜์—์„œ ์ถœ๋ฐœํ•˜๋Š” ๊ฒฝ์šฐ map์˜ ์ •๋ณด๊ฐ€ ๋ฐ”๋€Œ๊ฒŒ ๋œ๋‹ค. ์ด๋•Œ map์€ index๋ฅผ 5๋กœ ๋‚˜๋ˆˆ ๋ชซ๊ณผ ๋™์ผํ•˜๋ฏ€๋กœ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ง์˜ ์ •๋ณด๋ฅผ ๋ฐ”๊พผ๋‹ค.(20์ดํ•˜์ธ ์ด์œ ๋Š” 40์€ map์ด ๋ณ€ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ)

hores[i][0] += dice[cnt];
if(hores[i][1] == 0 && hores[i][0] >= map[0].length)
	hores[i][0] = 'e';
if(hores[i][1] > 0 && hores[i][1] < 4 && hores[i][0] >= map[hores[i][1]].length) {
	hores[i][0] = hores[i][0] - map[hores[i][1]].length + 1;
	hores[i][1] = 4;
}
if(hores[i][1] == 4 && hores[i][0] >= map[hores[i][1]].length) {
	hores[i][0] = hores[i][0] - map[hores[i][1]].length;
	hores[i][1] = 0;
	if(hores[i][0] > 0) hores[i][0] = 'e';
	else hores[i][0] = 20;
}

์ด๋™ํ•  map์„ ๊ฒฐ์ •ํ–ˆ์œผ๋ฏ€๋กœ ์ด์ œ ์ด๋™์„ ํ•ด์ค„ ๊ฒƒ์ด๋‹ค.

๋ง์˜ index์— dice๋ฅผ ๋”ํ•ด์„œ ์ด๋™ํ•ด์ฃผ๊ณ  ์ฒซ๋ฒˆ์งธ if๋ฌธ์œผ๋กœ ๋ง์„ ์ข…๋ฃŒ ์ง€์ ์— ๊ฐ€๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฐพ๋Š”๋‹ค.

๋‘๋ฒˆ์งธ if๋ฌธ์—์„œ map 1, 2, 3์ธ ๊ฒฝ์šฐ์ด๊ณ  index๊ฐ€ ์ตœ๋Œ€ ๊ธธ์ด๋ฅผ ๋„˜์–ด๊ฐ€๋Š” ๊ฒฝ์šฐ map 4๋กœ ์ด๋™ํ•ด์ค€๋‹ค. ์ด๋•Œ ์ด๋™ํ•œ ๊ธธ์ด๋งŒํผ ์ค„์—ฌ์ฃผ๋Š” ๊ฒƒ์„ ์žŠ์œผ๋ฉด ์•ˆ๋œ๋‹ค.

๋งˆ์ง€๋ง‰ if๋ฌธ์—์„œ ๋„์ฐฉํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ฒ˜๋ฆฌํ•ด์ค€๋‹ค.

์ด๋•Œ ๋งˆ์ง€๋ง‰ if๋ฌธ์€ else if๋กœ ์ฒ˜๋ฆฌํ•˜๋ฉด ์•ˆ๋œ๋‹ค! ๋‘๋ฒˆ์งธ์—์„œ ๋ณ€๊ฒฝ๋œ ๊ฐ’์ด ์„ธ๋ฒˆ์งธ if๋ฅผ ๋งŒ์กฑํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

if(hores[i][0] != 'e') {
				for(int k=0; k<4; k++)
					if(k == i) continue;
					else if(hores[k][0] == hores[i][0] && hores[k][1] == hores[i][1]) {
						hores[i][0] = x; hores[i][1] = m;
						continue c;
				}
			}

๋งˆ์ง€๋ง‰์œผ๋กœ ๋ง์˜ ์ •๋ณด๋“ค์„ ํ™•์ธํ•˜๋ฉฐ ํ˜„์žฌ ์œ„์น˜์— ๋‹ค๋ฅธ ๋ง์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•ด์ฃผ๊ณ  ์žˆ์œผ๋ฉด ์ฒ˜์Œ์— ์ €์žฅํ•œ ๊ฐ’์œผ๋กœ ๋‹ค์‹œ ๋˜๋Œ๋ ค์ค€๋’ค continue๋ฅผ ํ•ด์ค€๋‹ค.

			if(hores[i][0] == 'e')
				move(cnt+1, sum);
			else
				move(cnt+1, sum+map[hores[i][1]][hores[i][0]]);
			hores[i][0] = x; hores[i][1] = m;
		}
	}
}

๋ง์ด ๋งˆ์ง€๋ง‰ ๋„์ฐฉ์ ์— ๋‹ฟ๋Š”๋‹ค๋ฉด ์ ์ˆ˜๋ฅผ ์–ป์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— sum์— ์•„๋ฌด๊ฐ’๋„ ๋”ํ•˜์ง€ ์•Š๋Š”๋‹ค.

๊ทธ๊ฒŒ ์•„๋‹ˆ๋ผ๋ฉด ํ˜„์žฌ ๋จธ๋ฌผ๊ณ  ์žˆ๋Š” ์ฃผ์‚ฌ์œ„ ์ ์ˆ˜๋ฅผ ๋”ํ•˜์—ฌ ์žฌ๊ท€๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

Comments