Blue Bridge Cup-full array filter (Java)
Blue Bridge Cup every year must test the whole arrangement screening, generally is the blank question;
can use for the loop brute force hack, but the code is relatively long, also is disorderly, does not recommend the use;
Here the use of recursive solution, the amount of code is relatively small, but also very good understanding;
The following are: 0-9 of the total number of permutations;
If you want to implement from 0-9 to select n number only need to change the value of the DFS parameter k;
Full-lined Template code:
Public classMain {Static intCount=0; //Counter Public Static voidMain (string[] args) {int[] arr =New int[]{0,1, 2, 3,4,5,6,7,8,9}; The //array element is a number that needs to be fully arranged, and if you want to arrange the letters into a char array, the dfs (arr,0, 10); //Change to DFS (arr,0,5) is the selection of 5 numbers from the full array System.out.println (count); } //Recursive full arrangement Public Static voidDfsintArr[],intNumintk) {if(num = = k) {//Recursive exit if(Panduan (arr)) {//Filter//inside can output, count, according to the specific situation fillcount++; /*for (int i = 0; i < arr.length; i++) {System.out.print (arr[i]);
}system.out.println (); */ } } for(inti = num; i < arr.length; i++{swap (arr, num, i); DFS (arr, num+ 1, K); Swap (arr, num, i); } } // Filter Method Private Static BooleanPanduan (int[] arr) {
Judging the selection according to the condition of the topic
return false; } // array element Exchange, Note: A function can not be exchanged between variables Public Static voidSwapint[] arr,intIintj) {intt =Arr[i]; Arr[i]=Arr[j]; ARR[J]=T; }}
Examples:
Title: Five-Star filling number
such as "Figure 1.png" five-Star pattern node fill in the number: 1~12, remove 7 and 11.
Number and equality on each line.
is the proper method of filling.
Please use your computer to search for all possible methods of filling.
Note: The same fill is calculated after rotation or mirroring.
Note: rotate or mirror here: There are 5 different states of rotation (360 degrees of rotation encountered five consistent state), each state has 2 images (two-dimensional graphic image is generally 2), so the result is divided by 10;
Public classDemo2 {Static intCount=0; Public Static voidMain (string[] args) {intarr[]=New int[]{1,2,3,4,5,6,8,9,10,12};//Remove 7,11DFS (arr,0,10); System.out.println (Count/10); } Private Static voidDfsint[] arr,intNumintk) {//TODO auto-generated Method Stub if(num==k) { if(Panduan (arr)) {count++;//conditions reached, count++; } } for(inti=num;i<arr.length;i++) {swap (arr,num,i); DFS (Arr,num+1, K); Swap (arr,num,i); } } Private Static voidSwapint[] arr,intNuminti) {//TODO auto-generated Method Stub inttemp=Arr[num]; Arr[num]=Arr[i]; Arr[i]=temp; } Private Static BooleanPanduan (int[] arr) { //TODO auto-generated Method Stub//Judging five edges equal intA1=arr[0]+arr[2]+arr[5]+arr[8]; intA2=arr[1]+arr[2]+arr[3]+arr[4]; intA3=arr[0]+arr[3]+arr[6]+arr[9]; intA4=arr[1]+arr[7]+arr[5]+arr[9]; intA5=arr[4]+arr[6]+arr[7]+arr[8]; /*0 1 2 3 4 5 6 7 8 9*/ if(a1==a2&&a2==a3&&a3==a4&&a4==a5) { return true; }Else return false; }}
Full array filter (Java)