담배팔이소년

[BOJ] 22251 빌런 호석 본문

Algorithm

[BOJ] 22251 빌런 호석

bkkmw 2023. 1. 6. 23:44

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

 

22251번: 빌런 호석

LED를 2개까지 바꿀 수 있을 때, 5층에서 3층, 6층, 8층, 그리고 9층으로 바꿔버릴 수 있다.

www.acmicpc.net

 

문제 풀이

 

 

package boj;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

public class MainB_22251 {

	static boolean[][] seg_digit = new boolean[][] {
		{true, true, true, false, true, true, true},
		{false, false, false, false, false, true, true},
		{false, true, true, true, true, true, false},
		{false, false, true, true, true, true, true},
		{true, false, false, true, false, true, true},
		{true, false, true, true, true, false, true},
		{true, true, true, true, true, false, true},
		{false, false, true, false, false, true, true},
		{true, true, true, true, true, true, true},
		{true, false, true, true, true, true, true}
	};
	
	public static void main(String[] args) throws Exception{
		System.setIn(new FileInputStream("input/boj/input_22251.txt"));
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		StringTokenizer st = null;
		
		st = new StringTokenizer(br.readLine(), " ");
		int N = Integer.parseInt(st.nextToken());
		int K = Integer.parseInt(st.nextToken());
		int P = Integer.parseInt(st.nextToken());
		int X = Integer.parseInt(st.nextToken());
		
		int ans = solve(N, K, P, X);
		
		System.out.println(ans);
		
		br.close();
	}
	
	static int solve(int N, int K, int P, int X) {
		int ret = 0;
		int[][] toggle_cnts = get_toggle_cnts();
		int[] fnd = convert_to_digit(X);
		int[] limit = convert_to_digit(N);
		
		ret = change_digits(toggle_cnts, fnd, limit, 6-K, P, 0, X);
		
		return ret;
	}
	
	static int change_digits(int[][] toggle_cnts, int[] fnd, int[] limit, int idx, int P, int tcnt, int X) {
		int ret = 0;
		
		if(idx==6) {
			int n = convert_to_num(fnd,idx-1);
			return (n == 0 || n == X)? 0 : 1;
		}
		
		int fnd_src = fnd[idx];
		for(int i=0; i<10; i++) {
			if(tcnt + toggle_cnts[fnd_src][i] > P) continue;
			fnd[idx] = i;
			if(convert_to_num(fnd, idx) > convert_to_num(limit, idx)) continue;
			ret += change_digits(toggle_cnts, fnd, limit, idx+1, P, tcnt + toggle_cnts[fnd_src][i], X);
		}
		fnd[idx] = fnd_src;
		return ret;
	}
	
	
	static int[][] get_toggle_cnts(){
		int[][] ret = new int[10][10];
		for(int i=0; i<10; i++) {
			for(int j=i+1; j<10; j++) {
				int cnt = 0;
				for(int k=0; k<7; k++) {
					cnt += (seg_digit[i][k] != seg_digit[j][k])? 1 : 0;
				}
				ret[i][j] = cnt;
				ret[j][i] = cnt;
			}
		}
		return ret;
	}
	
	static int[] convert_to_digit(int num) {
		int[] ret = new int[6];
		for(int i=5; i>=0; i--) {
			ret[i] = num%10;
			num /= 10;
		}
		return ret;
	}
	
	static int convert_to_num(int[] digits, int idx) {
		int ret = 0;
		for(int i=0; i<=idx; i++) {
			ret *= 10;
			ret += digits[i];
		}
		return ret;
	}
	
}

 

'Algorithm' 카테고리의 다른 글

[BOJ] 01446 지름길  (0) 2023.01.15
[BOJ] 17143 낚시왕  (0) 2023.01.07
[BOJ] 17070 파이프 옮기기 1  (0) 2023.01.03
[BOJ] 07682 틱택토  (0) 2023.01.03
[BOJ] 17215 볼링 점수 계산  (0) 2023.01.02
Comments