算作一种备份吧
public class lq_2012_5 {
/**
* @param args
*/
//参数n为条件编号
public static void f(boolean[][] b, int n) {
if (n == 6) {
print(b);
}
if (n == 0) {
for (int i = 0; i < 7; i++)
for (int j = 0; j < 7 && j != i; j++)
for (int t = 0; t < 7 && t != i && t != j; t++) {
boolean[][] bb = new boolean[5][7];
for (int k = 0; k < 5; k++) {
bb[k][i] = true;
bb[k][j] = true;
bb[k][t] = true;
}
//
int[] c = new int[4];
int q = 0;
for (int m = 0; m < 7; m++)
if (m != i && m != j && m != t)
c[q++] = m;
for (int a1 = 0; a1 < 4; a1++)
for (int a2 = 0; a2 < 4 && a2 != a1; a2++)
for (int b1 = 0; b1 < 4; b1++)
for (int b2 = 0; b2 < 4 && b2 != b1; b2++)
for (int c1 = 0; c1 < 4; c1++)
for (int c2 = 0; c2 < 4 && c2 != c1; c2++)
for (int d1 = 0; d1 < 4; d1++)
for (int d2 = 0; d2 < 4
&& d2 != d1; d2++)
for (int e1 = 0; e1 < 4; e1++)
for (int e2 = 0; e2 < 4
&& e2 != e1; e2++) {
boolean[][] bbb = copy(bb);
bbb[0][c[a1]] = true;
bbb[0][c[a2]] = true;
bbb[1][c[b1]] = true;
bbb[1][c[b2]] = true;
bbb[2][c[c1]] = true;
bbb[2][c[c2]] = true;
bbb[3][c[d1]] = true;
bbb[3][c[d2]] = true;
bbb[4][c[e1]] = true;
bbb[4][c[e2]] = true;
f(bbb, n + 1);
}
}
}
if (n == 1) {
for (int i = 0; i < 5; i++) {
for (int t = 0; t < 7; t++) {
boolean f = (b[i][t % 7]) && (b[i][(t + 1) % 7])
&& (b[i][(t + 2) % 7]) && (b[i][(t + 3) % 7]);
if (f) {
return;
}
}
}
f(b, n + 1);
}
if (n == 2) {
if (!(b[0][2] && b[2][2])) {
return;
}
f(b, n + 1);
}
if (n == 3) {
if (b[1][6] || b[3][6] || b[4][6]) {
return;
}
f(b, n + 1);
}
if (n == 4) {
int j = 0;
for (int i = 0; i < 7; i++) {
if (b[0][i] && b[2][i])
j++;
}
if (j < 4) {
return;
}
f(b, n + 1);
}
if (n == 5) {
for (int i = 0; i < 7; i++) {
if ((!b[0][i] && !b[1][i] && !b[2][i] && !b[3][i])
|| (b[0][i] && !b[1][i] && !b[2][i] && !b[3][i])
|| (!b[0][i] && b[1][i] && !b[2][i] && !b[3][i])
|| (!b[0][i] && !b[1][i] && b[2][i] && !b[3][i])
|| (!b[0][i] && !b[1][i] && !b[2][i] && b[3][i])) {
return;
}
}
f(b, n + 1);
}
}
//打印函数
public static void print(boolean[][] b) {
for (int ii = 0; ii < 5; ii++) {
for (int jj = 0; jj < 7; jj++)
if (b[ii][jj])
System.out.print(1);
else
System.out.print(0);
System.out.println();
}
System.out.println();
}
//拷贝函数
public static boolean[][] copy(boolean[][] b) {
boolean[][] t = new boolean[5][7];
for (int i = 0; i < 5; i++)
for (int j = 0; j < 7; j++)
t[i][j] = b[i][j];
return t;
}
public static void main(String[] args) {
boolean[][] f = new boolean[5][7];
f(f, 0);
}
}