AcWing 291. 蒙德里安的梦想JAVA代码
原题链接
中等
作者:
ARM
,
2020-08-21 17:00:08
,
所有人可见
,
阅读 344
java 代码
import java.io.*;
import java.lang.*;
import java.util.*;
class Main{
static int n = 0, m = 0, N = 12, M = 1 << N;
static long[][] f = new long[N][M];
static boolean[] st = new boolean[M];
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
while(true){
String[] params = buf.readLine().split(" ");
if("0".equals(params[0]) || "0".equals(params[1]))break;
n = Integer.valueOf(params[0]);
m = Integer.valueOf(params[1]);
for(int i = 0; i < 1 << n; ++i){
int cnt = 0;//统计0出现的个数
st[i] = true;//假如当前位置合法
for(int j = 0; j < n; ++j)
if((i >> j & 1) != 0){
if((cnt & 1 ) != 0)st[i] = false;
cnt = 0;
}else cnt++;
if((cnt & 1 ) != 0)st[i] = false;
}
for(int i = 0; i < N; ++i){
Arrays.fill(f[i], 0);
}
f[0][0] = 1;
for(int i = 1; i <= m; ++i){
for(int j = 0; j < 1 << n; ++j){
for(int k = 0; k < 1 << n; ++k){
if((j & k) == 0 && st[j | k])
f[i][j] += f[i - 1][k];
}
}
}
System.out.println(f[m][0]);
}
}
}