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
}

CodePudding user response:

You can try search with memory

CodePudding user response:

reference 3 building self-confidence boy reply:
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
}


I want to only output a result, how to jump out of the recursion?

CodePudding user response:

reference 5 floor harryzhjx reply:
Quote: refer to the third floor confident boy reply:

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
}


I want to only output a result, how to jump out of the recursion?

Have a try?

In addition, according to your requirements definition out of position, when would you like to go out?

CodePudding user response:

reference 6 building self-confidence boy reply:
Quote: refer to the fifth floor harryzhjx reply:

The
Quote: 3 reference building self-confidence boy reply:

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
}


I want to only output a result, how to jump out of the recursion?

Have a try?

In addition, according to your requirements definition out of position, when would you like to go out?


I tried, still very slow

CodePudding user response:

Added in the SD times++, calculate the number of executions, and then analysis algorithm complexity, if really can't optimize, suggest to give up,

CodePudding user response:

refer to 7th floor harryzhjx response:
Quote: refer to the sixth floor confident boy reply:

Quote: refer to the fifth floor harryzhjx reply:

The
Quote: 3 reference building self-confidence boy reply:

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
}


I want to only output a result, how to jump out of the recursion?

Have a try?

In addition, according to your requirements definition out of position, when would you like to go out?


I tried, still very slow

If (n==180) {
//print (b);//print too slow, not
return;
}

CodePudding user response:

refer to the eighth floor proorck2019 response:
times++ is added in the SD, calculate the number of executions, and then analysis algorithm complexity, if really can't optimize, suggest to give up,


Ok, thank you
  • Related