Home > Back-end >  Depth first search
Depth first search

Time:10-05

Title:
Garlic you hand a few sticks, they differ in length, the garlic you want to use these sticks to spell out an equilateral triangle. And every stick should be used, for example, the head of jun Lin's hands have a length of 1, 2, 3, 3 of the 4 sticks. He can make a length of 1, 2 sticks of an edge. Another two, respectively, with two side, Mosaic an equilateral triangle of side length is 3, hope you can tell him ahead of the garlic king spell out, lest lost,
Input format: first enter an integer n, (3 & lt;=n & lt;=20), said the number of wood, then the length of the input n stick p (1 & lt;=p<=10000),
Output format: if the garlic can spell an equilateral triangle, output "yes", otherwise output "no",


Code:
 
//abstract depth-first search#include
using namespace std;
Int p [25];
Bool f;
int n;//input quantity
Int sum=0;
//x indicates how much finished edge
//s and how much is the current (used to compare with the sum/3)
//st said the next step index
Void DFS (int x, int s, int st) {
If (f) {
return;
}
If (x==3) {
F=true;
return;
}
If (s==sum/3) {

DFS (x + 1, 0, 0);
return;
}
For (int I=st. i //coutDFS (x, s + p [I], I + 1);
}
}
Int main () {
The scanf (" % d ", & amp; N);
for(int i=0; i The scanf (" % d ", & amp; P [I]);
The sum +=p [I];
}
If (sum % 3!=0) {
//that can not be split into equilateral
Printf (" \ n ");
} else {
DFS (0, 0, 0);
If (f) {
Printf (" yes \ n ");
} else {
Printf (" \ n ");
}
}
return 0;
}



Question: can you tell me the bosses,
 if (s==sum/3) {

DFS (x + 1, 0, 0);
return;
}

Here if the index of the next set to 0, wouldn't it be the edge of each piece is finished by the two sticks?
Bosses can explain, or the entire calculation steps

CodePudding user response:

This algorithm is problematic, where your code?
  • Related