Question Description:
Android's nine Gongge password must have been seen.
A password greater than or equal to four points is connected.
So how many kinds of nine password are there?
Algorithm Description:
keywords: combinatorial arrangement dynamic planning
Android's password is a 3x3 lattice of a path, this path can be crossed, can "walk the word", almost omnipotent (as long as not repeating points), but there is an exception: the path does not allow to skip the way to pass the point. For example, if you connect from the upper-left point to a point in the upper-right corner, that point in the middle is automatically added to the path. But the trouble is that the rule itself has a notable place: if the middle point is already used before, then this point can be skipped.
We might as well put the nine dots in the lattice number 1 to 9 respectively. According to the above rules, 4136, 4192 are illegal, but 24136, 654192 are feasible.
Algorithmic Thinking Reference: http://www.guokr.com/article/49408/
That is: Slash there are 8, a total of 8 pairs of numbers: 1-3 ago 2 1-7 is 4 1-9 need 5 2-8 need 5
3-7 Required 5 3-9 6 4-6 required 5 7-9 required 8 otherwise failed
So: through the dynamic planning traversal arrangement and verification (first if a sequence of I number combination is a failure, then its derived from the subsequent i+1 number of combinations of a sequence is also a failure, so no longer validate, that is, no further dynamic iteration)
The code runs:
password length |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
|
Number of passwords |
9 |
56 |
320 |
1624 |
7152 |
26016 |
72912 |
140704 |
140704 |
389497 |
Nine Gongge password is greater than or equal to four bits, so nine Gongge password a total of 389212, the password is best set 6 and more than 6.
The code is as follows:
1 Packagenine Gongge;2 3 Public classmain{4 5 Public Static voidMain (string[] argv) {6 7 //number of calculated combinations8 intK[] =New int[9];9 intP_count[] =New int[9];Ten intSum=0; One intOut_result=0; A - for(inti=0; i<9;i++){ - the if(i==0){ -k[i]=9; P_count[i]=1; - } - + Else{ -k[i]=k[i-1]* (9-i); + } A if(i>=3) atsum=sum+K[i]; - } - System.out.println (sum); - - //Calculation Arrangement - int[] middle =New int[9] [1]; in - for(inti=0; i<9;i++){ to + intn =K[i]; - intN =P_count[i]; the intMid_out[][] =New intN [I+1]; * intPai_out[][] =New intN [I+1]; $ intJ=0;Panax Notoginseng if(i==0){ - the for(; j<n; J + + ) +Pai_out[j][0]=0; AJ=0; the for(; j<n; J + +) +Mid_out[j][0]=j+1; -J=0; $ //update the last viable sequence number $K[i]=9; - } - Else{ the - //Get arrangementWuyi the for(intt=0; t<k[i-1]; t++){ - Wu intCheck[] =New int[10]; - for(intm=0; m<i;m++){ About $Check[middle[t][m]]=1; - - } - for(intP=1; p<=9; p++ ){ A + if(check[p]!=1){ the - //The alignment validation succeeds in adding to the sequence $ if(Check_success (middle[t][i-1],p,check)) { the the for(intm=0; m<i;m++){ themid_out[j][m]=Middle[t][m]; the } - inmid_out[j][i]=p; theJ + +; the } About the } the the } + - the }Bayi //update the last viable sequence number thek[i]=J; the } - if(i>2) -out_result=out_result+K[i]; theMiddle =New intN [I+1]; theMiddle =mid_out; the for(intcount_x=0; count_x<j; count_x++){ theSystem.out.print ((count_x+1) + "th:"); - for(intcount_y=0; count_y<=i;count_y++) theSystem.out.print (mid_out[count_x][count_y]+ ""); the System.out.println (); the }94System.out.println ("the" +i+ "th result as ..." +j); the the the }98 AboutSYSTEM.OUT.PRINTLN ("The result is:" +out_result); - for(inti=0; i<9; i++){101 102 System.out.println (K[i]);103 }104 } the 106 Public Static BooleanCheck_success (intAintBint[] r) {107 108 int[] k_table =New int[10] [10];109 //init ... thek_table[1][3]=2; k_table[3][1]=2; 111k_table[1][7]=4; K_table[7][1]=4; thek_table[1][9]=5; K_table[9][1]=5;113 //init .... thek_table[2][8]=5; K_table[8][2]=5; the //init .... thek_table[3][7]=5; K_table[7][3]=5; 117k_table[3][9]=6; K_table[9][3]=6; 118 //init ....119k_table[4][6]=5; K_table[6][4]=5; - //init ....121k_table[7][9]=8; K_table[9][7]=8;122 if(k_table[a][b]>0){123 if(r[k_table[a][b]]==1)124 return true; the Else126 return false;127 } - Else129 return true; the 131 the }133}
Nine Gongge cipher combination counting problem