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)。

posted on 2012-03-13 00:00  getgoing  阅读(453)  评论(0编辑  收藏  举报

导航