UVA155 UVALive5574 All Squares【递归】

Geometrically, any square has a unique, well-defined centre point. On a grid this is only true if thesides of the square are an odd number of points long. Since any odd number can be written in the form2k + 1, we can characterise any such square by specifying k, that is we can say that a square whosesides are of length 2k + 1 has size k. Now define a pattern of squares as follows.

  1. The largest square is of size k (that is sides are of length 2k + 1) and is centred in a grid of size1024 (that is the grid sides are of length 2049).

  2. The smallest permissible square is of size 1 and the largest is of size 512, thus 1 ≤ k ≤ 512.

  3. All squares of size k > 1 have a square of size ‘k div 2’ centred on each of their 4 corners. (‘div’implies integer division, thus ‘9 div 2 = 4’).

  4. The top left corner of the screen has coordinates (0, 0), the bottom right has coordinates (2048, 2048).

  Hence, given a value of k, we can draw a unique pattern of squares according to the above rules.Furthermore any point on the screen will be surrounded by zero or more squares. (If the point is onthe border of a square, it is considered to be surrounded by that square). Thus if the size of the largestsquare is given as 15, then the following pattern would be produced.


  Write a program that will read in a value of k and the coordinates of a point, and will determinehow many squares surround the point.

Input

Input will consist of a series of lines. Each line will consist of a value of k and the coordinates of apoint. The file will be terminated by a line consisting of three zeroes (0 0 0).

Output

Output will consist of a series of lines, one for each line of the input. Each line will consist of thenumber of squares containing the specified point, right justified in a field of width 3.

Sample Input

500 113 941

0 0 0

Sample Output

5


Regionals 1992 >> Europe - Northwestern


问题链接UVA155 UVALive5574 All Squares

问题简述:(略)

问题分析:读懂题意,找出递推式,用递归实现是常见的方法。先占个位置。

程序说明(略)

题记:(略)

参考链接:(略)


AC的C++语言程序如下:

/* UVA155 UVALive5574 All Squares */

#include <iostream>
#include <stdio.h>

using namespace std;

const int N = 1024;

int solve(int x, int y, int k, int px, int py)
{
    int dx, dy, ans;

    if(px <= x + k && px >= x - k && py <= y + k && py >= y - k)
        ans = 1;
    else
        ans = 0;

    if(px == x || py == y || k == 1)
        return ans;

    if(px < x)
        dx = -k;
    else
        dx = k;

    if(py < y)
        dy = -k;
    else
        dy = k;

    ans += solve(x + dx, y + dy, k / 2, px, py);

    return ans;
}

int main()
{
    int k, x, y;

    while(scanf("%d%d%d", &k, &x, &y) && (k || x || y)) {
        printf("%3d\n", solve(N, N, k, x, y));
    }

    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值