如五星图案节点填上数字:1~12,除去7和11。
要求每条直线上数字和相等。
如图就是恰当的填法。
请你利用计算机搜索所有可能的填法有多少种。
注意:旋转或镜像后相同的算同一种填法。
请提交表示方案数目的整数,不要填写任何其它内容。
思路:同样是全排列的题目,对数字进行全排列,然后把排列结果当成一位数组一样去看,我们根据图来确定那些下标要保证在一个组,判断一下是否符合条件,细节就是要旋转和镜像只算一种,那么旋转有五种,就除以五,对称有两种,就除以2.
public static void main(String[] args)
{
int[] select = new int[]{1,2,3,4,5,6,8,9,10,12};
LinkedList<Integer> path = new LinkedList<>();
dfs(path,select);
System.out.println(res.size()/5/2);
}
static LinkedList<LinkedList<Integer>> res = new LinkedList<>();
static void dfs(LinkedList<Integer> path,int[] select)
{
if(path.size()==10)
{
if(check(path))
res.add(new LinkedList<>(path));
return;
}
for(int i=0;i<select.length;i++)
{
if(path.contains(select[i]))
continue;
path.add(select[i]);
dfs(path,select);
path.removeLast();
}
}
static boolean check(LinkedList<Integer> path)
{
int a= path.get(0)+path.get(1)+path.get(2)+path.get(3);
int b= path.get(3)+path.get(4)+path.get(6)+path.get(7);
int c= path.get(7)+path.get(8)+path.get(1)+path.get(9);
int d= path.get(9)+path.get(2)+path.get(4)+path.get(5);
int e= path.get(5)+path.get(6)+path.get(8)+path.get(0);
if(a!=b || b!=c || c!=d || d!=e)
return false;
return true;
}