(数学)Knight's Trip -- hdu -- 3766

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

 

Knight's Trip

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 482    Accepted Submission(s): 106


Problem Description
In chess, each move of a knight consists of moving by two squares horizontally and one square vertically, or by one square horizontally and two squares vertically. A knight making one move from location (0,0) of an infinite chess board would end up at one of the following eight locations: (1,2), (-1,2), (1,-2), (-1,-2), (2,1), (-2,1), (2,-1), (-2,-1).

Starting from location (0,0), what is the minimum number of moves required for a knight to get to some other arbitrary location (x,y)?
 

 

Input
Each line of input contains two integers x and y, each with absolute value at most one billion. The integers designate a location (x,y) on the infinite chess board. The final line contains the word END.
 

 

Output
For each location in the input, output a line containing one integer, the minimum number of moves required for a knight to move from (0,0) to (x, y).
 

 

Sample Input
1 2、
2 4
END
 

 

Sample Output
1
2

 

/**
首先,xy的大小排序和转化为都是正数步数不变应该懂吧。
y=2*x这种情况直接就是(x+y)/3步。
如果y<2*x但是(x+y)%3==0的话,那么我们可以通过控制(1,2),(2,1)
两种跳法的次数达到...总数必然是(x+y)/3,然后xy的和对3取余是1的话,
我们是不是必然可以在(x+y-1)/3步的时候跳到(x,y-1)这个点,但是不能一步
跳到(x,y),回撤两步到(x-4,y-5)这个点,我们可以用三步跳到(x,y),那么
就是原先的步数+1。余数为2,就是先跳到(x-1,y-1)这个地方,我们知道(0,0)
到(1,1)只需要两步,那么(x-1,y-1)到(x,y)也就是原先步数+2.然后考虑y>2*x,
先把(0,1)的情况特殊处理一下。接着我们可以用x步跳到(x,y),那么原问题就
转化为(0,0)到(0,y-2*x)。当y-2*x是4的倍数的话我们可以直接(1,2)(-1,2)这个
跳可以在(y-2*x)/2步到达。余数为1,就是(0,0)到(0,1)的问题,但是这个需要
三步不是最优的,我们后撤两步变为(0,0)到(0,5),我们可以三步达到,那么就
是原先的步数加上1就是解。余数为2,我们可以分别跳一次(2,1)(-2,1)到达。
余数为3,转化为(0,0)到(0,3)的问题我们可以(-1,2)(1,1)(0,3)三步到达。
以上就是全部情况,o(╯□╰)o,在纸上画画,应该所有在这这几类范围内。
**/



#include<stdio.h>
#include<string.h>
int main()
{
    char c[20];

    while(scanf("%s", c), strcmp(c, "END"))
    {
        int x, y;
        int k;

        sscanf(c, "%d", &x);
        scanf("%d", &y);

        if(x<0) x = -x;
        if(y<0) y = -y;

        if(y<x) {k=x, x=y, y=k;}
        if(y<=2*x)
        {
            if(x==1 && y==1)
                printf("2\n");
            else if(x==2 && y==2)
                printf("4\n");
            else
                printf("%d\n", (x+y)/3+(x+y)%3);
        }
        else
        {
            int 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;
}

 

转载于:https://www.cnblogs.com/YY56/p/4954115.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值