Knight ' s Trip---hdu3766 (the minimum number of steps to go on horse walking day)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3766

Give you an X, y find out from (0,0) position to reach the minimum number of steps required each time can only walk the Japanese type;

The answer to the violent bfs;

/** first of all, the size of XY is sorted and converted to a positive number of steps the same should be understood. Y=2*x This situation is directly (X+y)/3 steps. If Y<2*x but (x+y)%3==0, then we can control (by), (2,1) The number of two hops to reach ... The total number must be (x+y)/3, and then XY and 3 to take the remainder is 1, we are not bound to (X+Y-1)/3 steps to jump to (x,y-1) this point, but can not jump to (x, y), two steps back to (x-4,y-5) This point, we can use three steps to jump to (x, y) , then the original number of steps is +1. The remainder is 2, which is to jump first to (x-1,y-1) This place, we know (0,0) to (to) only two steps, then (X-1,y-1) to (x, y) is the original step +2. Then consider the y>2*x, first of all (0,1) the case of special treatment. Then we can jump to the (x, y) step with the 0,0, then the original problem is converted to (0,y-2*x). When Y-2*x is a multiple of 4 we can directly ( -1,2) This jump can arrive at (Y-2*X)/2 steps. The remainder is 1, that is (0,0) to (0,1) the problem, but this need three steps is not optimal, we withdraw two steps into (0,0) to (0,5), we can achieve three steps, then is the original number of steps plus 1 is the solution. The remainder is 2, and we can jump once (2,1) ( -2,1) to arrive. The remainder is 3, converted to (0,0) to (0,3) The problem we Can ( -1,2) () (0,3) Three steps to arrive. The above is the whole situation, O (╯-╰) O, drawing on paper, should be all within these categories of scope. **/#include <stdio.h> #include <iostream> #include <string.h> #include <algorithm> #include    <math.h>using namespace std; #define N 20100int Main () {int x, y, ans;    Char s[110];        while (scanf ("%s", s), strcmp (S, "END")) {sscanf (S, "%d", &x);        scanf ("%d", &y);        X=abs (x);        Y=abs (y);        if (x>y) swap (x, y); If(y==x*2)            {printf ("%d\n", (x+y)/3);        Continue            } if (y<=2*x) {if (x==1&&y==1) ans = 2;            else if (x==2&&y==2) ans = 4;        else ans = (x+y)/3+ (x+y)%3;            } else {ans=x;            int c= (y-2*x)%4;            Ans+=c;            ans+= (Y-2*X-C)/2;        if (y==1&&x==0) ans=3;    } printf ("%d\n", ans); } return 0;}

Knight ' s Trip---hdu3766 (the minimum number of steps to go on horse walking day)

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.