[Algorithm] ๐Ÿ•›์‹œ๊ณ„ ๋งž์ถ”๊ธฐ

HaJingJingยท2021๋…„ 3์›” 28์ผ
0

Algorithm

๋ชฉ๋ก ๋ณด๊ธฐ
3/119

1. ๋ฌธ์ œ ํ•ด์„

16๊ฐœ์˜ ์‹œ๊ณ„๊ฐ€ ์กด์žฌํ•˜๊ณ  ๋ชจ๋‘ 12์‹œ๋กœ ๋งž์ถฐ์•ผํ•จ

๊ฐ ์‹œ๊ณ„๋“ค์€ ํ•˜๋‚˜์˜ ๋ฒ„ํŠผ์— ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด์„œ ๋ฒ„ํŠผ์„ ํ•œ ๋ฒˆ ๋ˆ„๋ฅด๋ฉด 3์‹œ๊ฐ„ ํ›„๋กœ ์ด๋™ํ•จ

10๋ฒˆ ์ด๋‚ด์— ์‹œ๊ณ„ ๋งž์ถ”๊ธฐ

์ž…๋ ฅ

C(ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค), 16๊ฐœ ์‹œ๊ณ„์˜ ์‹œ๊ฐ„

์ถœ๋ ฅ

12์‹œ๋กœ ๋งž์ถ”๊ธฐ ์œ„ํ•ด ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋Š” ์ตœ์†Œ ํšŸ์ˆ˜ (10ํšŒ ์ดํ•˜)


2. ์•„์ด๋””์–ด

๐Ÿ’ก ํ•œ ๊ฐœ์˜ ์Šค์œ„์น˜๋Š” 4๋ฒˆ ์ด์ƒ ๋ˆ„๋ฅด์ง€ ์•Š์Œ (๋‹ค์‹œ ์›์ƒํƒœ๋กœ ๋ณต๊ตฌ)
๐Ÿ’ก ์Šค์œ„์น˜์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์‹œ๊ณ„๋ฅผ ๋ฐฐ์—ด๋กœ ์ €์žฅํ•ด์„œ ์ด์šฉ
๐Ÿ’ก ์žฌ๊ท€ํ•จ์ˆ˜ ์‚ฌ์šฉํ•˜์—ฌ ๊ฐ๊ฐ์˜ ์Šค์œ„์น˜๋ฅผ 0-3๋ฒˆ๊นŒ์ง€ ๋ˆŒ๋Ÿฌ๋ณด๊ณ  ๊ฐ€์žฅ ์ž‘์€ ๊ฐ’์„ ๊ฒฐ๊ณผ๊ฐ’์— ๋„ฃ์Œ


3. ํ’€์ด

1) ์Šค์œ„์น˜์™€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋Š” ์‹œ๊ณ„๋ฅผ ๋ฐฐ์—ด๋กœ ์„ ์–ธ

static int linked[][] = {
			{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0},
			{0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1},
			{1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,1,1,1,0,1,0,1,0,0,0},
			{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1},
			{0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1},
			{0,0,0,0,1,1,0,1,0,0,0,0,0,0,1,1},
			{0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,1,1,1,0,0,0,1,0,0,0,1,0,0}
	};

2) ๊ฐ๊ฐ์˜ ์Šค์œ„์น˜ 0-3๋ฒˆ ๋ˆŒ๋Ÿฌ๋ณด๊ณ , ret์— ์ž‘์€ ๊ฐ’ ์ €์žฅ

int ret = INF;
for(int i=0; i<4; i++) {
		ret = Math.min(ret, i+solve(clocks,swtch+1));
		push(clocks,swtch);
}

4. ์ฝ”๋“œ

import java.util.*;

public class ClockSync {
	static int INF = 9999;
	static int SWITCH = 10;
	static int CLOCK = 16;
	static int linked[][] = {
			{1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,1,0,0,0,1,0,1,0,1,0,0,0,0},
			{0,0,0,0,1,0,0,0,0,0,1,0,0,0,1,1},
			{1,0,0,0,1,1,1,1,0,0,0,0,0,0,0,0},
			{0,0,0,0,0,0,1,1,1,0,1,0,1,0,0,0},
			{1,0,1,0,0,0,0,0,0,0,0,0,0,0,1,1},
			{0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,1},
			{0,0,0,0,1,1,0,1,0,0,0,0,0,0,1,1},
			{0,1,1,1,1,1,0,0,0,0,0,0,0,0,0,0},
			{0,0,0,1,1,1,0,0,0,1,0,0,0,1,0,0}
	};
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int c = sc.nextInt();
		int clocks[] = new int[CLOCK];
		int result[] = new int[c];
		
		for(int i=0; i<c; i++) {
			for(int j=0; j<CLOCK; j++) {
				clocks[j] = sc.nextInt();
			}
			
			result[i] = solve(clocks,0);
			if(result[i] == INF)
				result[i] = -1; 
		}
		
		for(int i=0; i<c; i++)
			System.out.println(result[i]);
	}
	
	public static boolean areAligned(int clocks[]) {
		for(int i=0; i<CLOCK; i++) {
			if(clocks[i] != 12)
				return false;
		}
		
		return true;
	}
	
	public static void push(int clocks[], int swtch) {
		for(int clock=0; clock<CLOCK; clock++) {
			if(linked[swtch][clock] == 1) {
				clocks[clock] += 3;
				if(clocks[clock] == 15)
					clocks[clock] = 3;
			}
		}
	}
	
	public static int solve(int clocks[], int swtch) {
		if(swtch == SWITCH)
			return areAligned(clocks) ? 0:INF;
		
		int ret = INF;
		for(int i=0; i<4; i++) {
			ret = Math.min(ret, i+solve(clocks,swtch+1));
			push(clocks,swtch);
		}
		
		return ret;
	}

}

5. ๊ฒฐ๊ณผ


์„ฑ๊ณต!..

profile
๐ŸŒฑ์ดˆ๋ณด ๊ฐœ๋ฐœ์ž๐ŸŒฑ

0๊ฐœ์˜ ๋Œ“๊ธ€