Home > Back-end > Help to look at how to optimize the code
Help to look at how to optimize the code
Time:10-04
# include & lt; stdio.h> # include & lt; Stdlib. H>
Int count=0; Void print (int a [30] [6]) { count++; Printf (" case: % d: \ n ", count); for(int i=0; i<30; I++) for(int j=0; j<6; J++) { Printf (" % d ", a [I] [j]); J==5? Printf (" % d \ n ", I) : 0; }
} //determine whether the line, I can be the first j column is set to k Int test (int a [30] [6], int, int j, int k) { Int p, q; P=0; While (p<6) { If (a [I] [p]==k) return 0; The else + + p; }
Int num1=1, col=0; While (colFor (q=0; Q If ((a [q] [col]==a [I] [col] & amp; & A [q] [j]==k) | | (a [q] [col]==k& & A [q] [j]==a [I] [col])) + + num1; } If (num1 & gt; 2) return 0; The else { Col++; Num1=1; } } return 1; }
Void SD (int a [30] [6], int n)//solving { int i,j; Int b [30] [6]. for(i=0; i<30; I++) for(j=0; j<6; J++) B [j]=[I] a [I] [j];//try using b I=n/6, j=n % 6;//list If (n==180) { Print (b); } If (a [I] [j].=0) { SD (b, n + 1); A [I] [j]=0; }//if the position fixed
The else { Int k=1;//try several While (k<{ =6)If (test (b, I, j, k))//can { B [I] [j]=k; SD (b, n + 1); B [I] [j]=0; } K++; }
}
} Int main () { Int a [30] [6]. Printf (" please enter the original data, replace with 0, no data \ n "); for(int i=0; i<30; I++) for(int j=0; j<6; J++) The scanf (" % d ", & amp; A [I] [j]); Printf (" -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- \ n \ n "); SD (a, 0); If (count==0) Printf (" this array with no solution!" ); return 0; }
CodePudding user response:
Long time operation, hope ace to help and advice, simplify it
CodePudding user response:
You call again in the SD and SD, probably this is the cause of the slow,
CodePudding user response:
Recursive functions suggest clear exit criteria, see you the if, else calls itself, how to exit the SD?
if (n==180) { Print (b); return;//that out whether meet your requirements, if conform to suggest try }