如下的10个格子
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。
填入0~9的数字。要求:连续的两个数字不能相邻。
(左右、上下、对角都算相邻)
一共有多少种可能的填数方案?
import java.util.Stack;
public class _javaB06 {
public static int count = 0;
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
Stack<Integer> stack = new Stack<>();
fun(stack);
long endTime = System.currentTimeMillis();
System.out.println("时间: " + (endTime - startTime));
System.out.println("count=" + count);
}
private static void fun(Stack<Integer> stack) {
if (stack.size() == 10) {
int A[] = new int[10];
A[0] = stack.get(0);
A[1] = stack.get(1);
A[2] = stack.get(2);
A[3] = stack.get(3);
A[4] = stack.get(4);
A[5] = stack.get(5);
A[6] = stack.get(6);
A[7] = stack.get(7);
A[8] = stack.get(8);
A[9] = stack.get(9);
boolean one = isNeibor(A[0], A[1], A[5], A[4], A[3]) && isNeibor(A[1], A[0], A[2], A[6], A[5], A[4]) && isNeibor(A[2], A[1], A[5], A[6]);
boolean two = isNeibor(A[3], A[0], A[4], A[8], A[7]) && isNeibor(A[4], A[0], A[8], A[5], A[9], A[8], A[7], A[3])
&& isNeibor(A[5], A[0], A[1], A[2], A[6], A[9], A[8], A[4]) && isNeibor(A[6], A[2], A[1], A[5], A[9]);
boolean three = isNeibor(A[7], A[8]) && isNeibor(A[8], A[9]);
if (one && two && three) {
System.out.printf("%d,%d,%d, %d,%d,%d,%d, %d,%d,%d\n", A[0], A[1], A[2], A[3], A[4], A[5], A[6], A[7], A[8], A[9]);
count++;
}
return;
}
for (int j = 0; j < 10; j++) {
if (!stack.contains(j)) {
stack.push(j);
fun(stack);
stack.pop();
}
}
}
public static boolean isNeibor(int ...args){
int first = args[0];
for (int i = 1; i < args.length; i++) {
if (Math.abs(first - args[i]) == 1) {
return false;
}
}
return true;
}
}
答案:1580
请填写表示方案数目的整数。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。