Solved.ac Class3++
public class Main {
private static int size;
private static boolean[][] square;
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
size = Integer.parseInt(br.readLine());
square = new boolean[size][2];
int solve = solve(0, 0);
System.out.println(solve);
}
private static int solve(int x, int y) {
int answer = 0;
if (y == 0 && !square[x][y + 1]) {// 1*2
square[x][y] = true;
square[x][y+1] = true;
if (x + 1 < size) {
answer += solve(x + 1, y);
} else {
answer += 1;
}
square[x][y] = false;
square[x][y+1] = false;
}
if (x + 1 < size && !square[x + 1][y] && y == 0 && !square[x][y + 1] && !square[x + 1][y + 1]) { // 2*2, 2*1
square[x][y] = true;
square[x + 1][y] = true;
square[x][y+1] = true;
square[x + 1][y + 1] = true;
if (x + 2 < size) {
answer += 2 * solve(x + 2, y);
} else {
answer += 2;
}
square[x][y] = false;
square[x + 1][y] = false;
square[x][y+1] = false;
square[x + 1][y + 1] = false;
}
return answer;
}
}
시간초과
public class Main {
public static void main(String[] args) throws Exception{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int size = Integer.parseInt(br.readLine());
int[] data = new int[1000];
data[0] = 1;
data[1] = 3;
data[2] = 5;
for (int i = 3; i < size; i++) {
data[i] = (data[i - 1] + (data[i - 2] * 2)) % 10007;
}
System.out.println(data[size - 1]);
}
}
점화식을 이용하여 해결하였다
성공
fun main() {
val size = readln().toInt()
val data = IntArray(1000)
data[0] = 1
data[1] = 3
data[2] = 5
for (i in 3..<size) {
data[i] = (data[i - 1] + (2 * data[i - 2])) % 10007
}
print(data[size - 1])
}