Fork Copy https://www.geeksforgeeks.org/samsung-semiconductor-institute-of-research-ssir-software-intern-fte-set-1/ #include using namespace std; int NSpot; int totalPerson; struct Gate{ int toado; int numPerson; }; Gate gate[4]; int tc,T; int markGate[4]; int PositionSpot[100]; int rs; int abs(int val){ if(val<0) return -val; return val; } void backtrack(int numGate,int numPerInGate,int total,int cnt){ if(numGate>3) return; if(total>=totalPerson){ if(cnt=gate[numGate].numPerson){ markGate[numGate] = 1; backtrack(numGate+1,0,total,cnt); markGate[numGate] = 0; } for(int i=1;i<=3;i++){ //thu tung gate if(!markGate[i]){//neu cong gate chua mo thi mo de cho nguoi vao int numPer = gate[i].numPerson; for(int j=1;j<=NSpot;j++){ if(!PositionSpot[j]){//neu vi tri cau ca con trong thi cho vao PositionSpot[j] = gate[numGate].toado; backtrack(numGate,numPerInGate+1,total+1,cnt+abs(gate[i].toado-j)+1); PositionSpot[j] = 0; } } } } } void initPosSpot(){ for(int i=1;i<=NSpot;i++) PositionSpot[i] = 0; } int main() { freopen("fishing.txt","r",stdin); cin>>T; for(tc=1;tc<=T;tc++){ cin>>NSpot; totalPerson = 0; initPosSpot(); rs = ~(1<<31); for(int i=1;i<=3;i++){ markGate[i] = 0; cin>>gate[i].toado; } for(int i=1;i<=3;i++){ cin>>gate[i].numPerson; totalPerson+=gate[i].numPerson; } backtrack(1,0,0,0); cout< Gate 2 > Gate 3 For this case, the sum of moving distance is : 3+2+1+2+3+2+3+2+1 = 19 Case 2) We open the gate by the order : Gate 2 > Gate 1 > Gate 3 When open Gate 3, the 1st customer will go to spot 6, the second one can go to spot 5 or 7 OR Case 2-1) In this case, the sum is : 4+3+2+1+2+1+4+2+1 = 20 Case 2-2) In this case, the sum is : 4+3+2+1+2+1+2+2+1 = 18 [Input] - The first line given the number of test case T (T <= 50) - For each test case: + The first line given the number of spots N (10 <= N <= 60) + The next three lines give the information of 3 gates : > Gate's position P ( 1 <= P <= N) > The number of customers standing before that gate C ( 1 <= C <= 20 ) [Output] The minimum moving distance of all customers Case #1 18 Case #2 25 Case #3 57 Case #4 86 Case #5 339