51nod1489 (DFS)

Source: Internet
Author: User

Title Link: https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1489

Test instructions: Chinese question eh ~

Idea: DFS

First we will kill the first and last person by attacking the 1th and the 2nd person.

Next we consider how to kill the middle man most efficiently:

Assuming that we are attacking the POS person at the moment, then we have to make sure that the pos-1 person is dead to move down a person (this is well understood, because we cannot kill it if we have not killed the first pos-1 person before attacking the POS person). But we need to be more specific about these situations:

1. If we die at the point where the POS person dies, the pos person is dead and we have not killed the pos-1 person, then we need to continue attacking the POS individual until pos-1 death (because we are using DFS, so there is no need to consider returning to direct attack on the pos-1 person);

2. If we attack the POS person, POS is not dead, pos-1 is dead, assuming we attack CNT kill pos-1, Attack cc times can kill Pos, then we need to traverse the attack cnt~cc attacks into the next level of recursion all the circumstances;

The best solution we've got is the answer.


1#include <bits/stdc++.h>2 #defineMAXN 503 #defineINF 0x3f3f3f3f4 using namespacestd;5 6 intGG[MAXN], ans=INF, n, a, B;7 8 voidDfsintPosintnum) {9     intCnt=0, cc=0;//* * * Note here the CNT must be assigned an initial value, because it is possible to meet the (cc>cnt&&gg[pos]>=0) condition when the condition is not satisfied (gg[pos-1]>=0)Ten     if(pos==n) {//* * * to reach nth person is to return and update the number of attacks Oneans=min (ans, num); A         return; -     } -     if(gg[pos-1]<0){//* * * Only the previous person is dead to move backwards theDFS (pos+1, num); -     } -     if(gg[pos-1]>=0){//* * * Handling only cases greater than or equal to 0 -cnt=gg[pos-1]/b+1; +gg[pos-1]-=b*CNT; -gg[pos]-=a*CNT; +gg[pos+1]-=b*CNT; ADFS (pos+1, num+CNT);  atgg[pos-1]+=b*cnt;//* * * pay attention to backtracking to restore the amount of the previous change -gg[pos]+=a*CNT; -gg[pos+1]+=b*CNT; -     } -cc=gg[pos]/a+1; -     if(cc>cnt&&gg[pos]>=0){//* * * If pos-1 is dead at this point and POS is still alive, we can choose to kill it directly and move it to the next one or move it directly down a in          for(inti=cnt+1; i<=cc; i++){ -gg[pos-1]-=b*i; togg[pos]-=a*i; +gg[pos+1]-=b*i; -DFS (pos+1, num+i); thegg[pos-1]+=b*i; *gg[pos]+=a*i; $gg[pos+1]+=b*i;Panax Notoginseng         } -     } the     return; + } A  the intMainvoid){ +     intnum=0; -CIN >> N >> a >>b; $      for(intI=1; i<=n; i++){ $CIN >>Gg[i]; -     } -     intcnt=gg[1]/b+1;//* * * First go head thenum+=CNT; -gg[1]-=b*CNT;Wuyigg[2]-=a*CNT; thegg[3]-=b*CNT; -     if(gg[n]>=0){//* * * to the end, note that if only three people can be gg[n] this time is less than 0 Wucnt=gg[n]/b+1; -num+=CNT; Aboutgg[n-2]-=b*CNT; $gg[n-1]-=a*CNT; -gg[n]-=b*CNT; -     } -Dfs2,0); A     if(Ans==inf) {//* * * Note there is a possibility that DFS directly satisfies the condition exit, at which time the ANS value is INF +ans=0; the     } -cout << Ans+num <<Endl; $     return 0; the}

51nod1489 (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.