Prairie dogs

作者在 2007-06-01 18:08:00 发布以下内容
Oh, my God! The lovely prairie dogs come again! We know they are very naughty and always play some funny games. This time, they play a game named Spiral Queue.

Each of the prairie dogs has a number and they stand in a funny queue named Spiral Queue like in Figure 1.

Given the coordinate, the direction of x-axis and y-axis is indicated in Figure 2. We suppose the coordinate of 1 is (0,0), then the coordinate of 2 is (1,0), the coordinate of 3 is (1,1) and the coordinate of 7 is (-1,-1).

21   22  ...
20   7   8   9  10
19   6   1   2  11
18   5   4   3  12
17  16  15  14  13
      
      Figure1
Find out the laws of the Spiral Queue.

Your task is here: Given x and y (-1000 ≤ x,y ≤ 1000), the coordinate of a prairie dog, try to find out the number of the prairie dog.

Input

Each line of input will have two numbers x and y, indicating the coordinate of a prairie dog. The input stops when EOF (end of file) is reached.

Output

You must output the number of the prairie dog based on the coordinate (x,y), followed by a newline.

Sample Input

0 0
-1 -1
1 0
1 1
100 100
1000 -1000

Sample Output

1
7
2
3
39801
4004001
经典程序 | 阅读 2805 次
文章评论,共3条
ecbtnrt(作者)
2007-06-01 18:12
1
#include        <stdio.h>


int x_max(int m ,int n)
{
    if(m < 0)
        m = -m;
    if(n < 0)
        n = -n;
    if(m > n)
        return m;
    return n;
}

int main(void)
{
    int x, y;
    int sum = 0;
    int t;
    int i;

    scanf("%d%d", &x, &y);
    t = x_max(x, y);
    sum = 4 * (t -1)*(t - 1) + 4 * (t - 1) + 2 ;

    for( i = 0; i <= (2*t-1); i++)
    {
        if(y == -t+1+i)
        {
            if(x == t)
            {
                sum += i;
                printf("%d\n", sum);
                return 0;
            }
        }
    }
    sum += 2*t-1;

    for(i = 1; i <= 2*t; i++)        
    {
        if(x == t - i)
        {
            if(y == t)
            {
                sum += i;
                printf("%d\n", sum);
                return 0;
            }
        }
    }
    sum += 2*t;

    for(i = 1; i <= 2*t; i++)            
    {
        if(y == t - i)
        {
            if(x == -t)
            {
                sum += i;
                printf("%d\n", sum);
                return 0;
            }
        }
    }
    sum += 2*t;

    for(i = 0; i <= 2*t; i++)
    {
        if(x == -t+i)
        {
            sum += i;
            printf("%d\n", sum);
            return 0;
        }
    }
    printf("%d\n", sum );
    return 0;

}
ecbtnrt(作者)
2007-06-01 18:13
2
这是我写的程序,运行是正确的。不过如果提交的话有点问题,我搞不懂为什么。你能指导我一下吗
ecbtnrt(作者)
2007-06-04 08:34
3
我想我找到原因了,是不是要把输入输出改成文件得形式
游客请输入验证码
浏览107743次