csu 1011 Counting Pixels

没想象中的复杂;

先将园分为四块以减少计算量(还可以再细分为八块,略显复杂);

判断圆是如何穿过一个像素的:对于右上方的点,只有三种情况:从右边穿过,从右下角的顶点穿过,从下边穿过;

先给出一个初始像素(被穿过),然后根据穿过的情况向下一个被穿过的像素扩展,并根据情况计数;

像素的表示:右上方定点的坐标:

    Language: C
    Result: Accepted
    Time:236 ms
    Memory:740 kb
最快的只有128ms,求指点。
 1 # include <stdio.h>
2
3 int x, y, r;
4
5 int state(int x, int y); // 1: right, 0: corner, -1: bottom
6
7 int main()
8 {
9 long long cnt;
10
11 freopen("in.txt", "r", stdin);
12 freopen("out.txt", "w", stdout);
13
14 while (~scanf("%d%d%d", &x, &y, &r))
15 {
16 if (!(x || y || r)) break;
17 cnt = 0;
18 x = 1, y = r;
19 while (x <= r && y >= 1)
20 {
21 if (state(x, y) == 1) ++cnt, --y;
22 else if (state(x, y) == 0) cnt += y, ++x, --y;
23 else if (state(x, y) == -1) cnt += y, ++x;
24 else printf("wrong\n");
25 }
26 printf("%lld\n", cnt<<2);
27 }
28
29 return 0;
30 }
31
32 int state(int x, int y)
33 {
34 int t1, t2;
35 --y;
36 t2 = r*r;
37 t1 = x*x + y*y;
38 if (t1 > t2) return 1;
39 if (t1 == t2) return 0;
40 if (t1 < t2) return -1;
41 }
2012/3/13  00:10
在上述代码的第19行的循环下,可以设置一个临时变量tmp, 保存state(x, y)的值,减少耗时(200ms)。

转载于:https://www.cnblogs.com/JMDWQ/archive/2012/03/13/2392684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值