您的位置:首页 > 其它

蓝桥杯 第七届省赛试题 方格填数

2016-04-08 21:01 197 查看
package com.diqijie.shengsai;

/**
* @author leibaobao
*方格填数
*解:没想太多,全排列能做的,加几个判断就好。想多了浪费时间
*/
public class _6 {
public static int count = 0;
public static void swap(char array[], int a, int b) {
char temp = array[a];
array[a] = array[b];
array[b] = temp;
}

public static void Permutation(char[] str, int a, int length) {
if (a == length) {
if (jud(str)) {
count++;
System.out.println(String.valueOf(str));
}
} else {
for (int i = a; i <= length; i++) {
swap(str, i, a);
Permutation(str, a + 1, length);
swap(str, i, a);
}
}
}

private static boolean jud(char [] str) {
if(jus1(str[0], str[1]) && jus1(str[0], str[3]) && jus1(str[0], str[4]) && jus1(str[0], str[5])
&& jus1(str[1], str[2]) && jus1(str[1], str[4]) && jus1(str[1], str[5]) && jus1(str[1], str[6])
&& jus1(str[2], str[5]) && jus1(str[2], str[6])
&& jus1(str[3], str[4]) && jus1(str[3], str[7]) && jus1(str[3], str[8])
&& jus1(str[4], str[5]) && jus1(str[4], str[7]) && jus1(str[4], str[8]) && jus1(str[4], str[9])
&& jus1(str[5], str[6]) && jus1(str[5], str[8]) && jus1(str[5], str[9])
&& jus1(str[6], str[9]) && jus1(str[7], str[8]) && jus1(str[8], str[9]))
return true;
return false;
}

private static boolean jus1(char c, char d) {
// TODO Auto-generated method stub
if(c == (d+1) || c == (d-1))
return false;
return true;
}

public static void main(String[] args) {
char[] str = "0123456789".toCharArray();
Permutation(str, 0, 9);
System.out.println(count);
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: