😈 [22251] 빌런 호석
난이도: 골드 5
소요 시간: 92분
메모리: 12004KB
시간: 100ms
- 최대 층수 체크하기
- 숫자 별로 바뀌는 led 개수 계산해 저장하기
- 최대 자리수 체크하기
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main_22251_빌런호석 {
static int[][] arr;
static int answer, P, K;
static char[] arrX, arrN;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
String strN = st.nextToken().trim();
K = Integer.parseInt(st.nextToken());
P = Integer.parseInt(st.nextToken());
String strX = st.nextToken().trim();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < K - strN.length(); i++) {
sb.append("0");
}
arrN = sb.append(strN).toString().toCharArray();
sb = new StringBuilder();
for (int i = 0; i < K - strX.length(); i++) {
sb.append("0");
}
arrX = sb.append(strX).toString().toCharArray();
answer = 0;
check();
changeLED(0, P, 0, 0);
System.out.print(answer);
br.close();
}
static void changeLED(int nth, int count, int N, int floor) {
if (nth == K) {
if (0 < floor && count < P) {
answer++;
}
return;
}
int target = arrX[nth] - '0';
int curN = N * 10 + arrN[nth] - '0';
int curFloor = floor * 10;
for (int i = 0; i <= 9; i++) {
if (curN < curFloor + i) {
break;
}
if (arr[target][i] <= count) {
changeLED(nth + 1, count - arr[target][i], curN, curFloor + i);
}
}
}
static void check() {
arr = new int[10][10];
boolean[][] number = {
{true, true, true, true, true, true, false},
{false, true, true, false, false, false, false},
{true, true, false, true, true, false, true},
{true, true, true, true, false, false, true},
{false, true, true, false, false, true, true},
{true, false, true, true, false, true, true},
{true, false, true, true, true, true, true},
{true, true, true, false, false, false, false},
{true, true, true, true, true, true, true},
{true, true, true, true, false, true, true}
};
int cnt;
for (int i = 0; i < 10; i++) {
for (int j = i + 1; j < 10; j++) {
cnt = 0;
for (int z = 0; z < 7; z++) {
if (number[i][z] != number[j][z]) {
cnt++;
}
}
arr[i][j] = arr[j][i] = cnt;
}
}
}
}