csuoj-1011-Counting Pixels

题目:

Description

Did you know that if you draw a circle that fills the screen on your 1080p high definition display, almost a million pixels are lit? That‘s a lot of pixels! But do you know exactly how many pixels are lit? Let‘s find out!

Assume that our display is set on a Cartesian grid where every pixel is a perfect unit square. For example, one pixel occupies the area of a square with corners (0,0) and (1,1). A circle can be drawn by specifying its center in grid coordinates and its radius. On our display, a pixel is lit if any part of it is covered by the circle being drawn; pixels whose edge or corner are just touched by the circle, however, are not lit.

Your job is to compute the exact number of pixels that are lit when a circle with a given position and radius is drawn.

Input

The input consists of several test cases, each on a separate line. Each test case consists of three integers, x,y, and r(1≤x,y,r≤1,000,000), specifying respectively the center (x,y) and radius of the circle drawn. Input is followed by a single line with x = y = r = 0, which should not be processed.

Output

For each test case, output on a single line the number of pixels that are lit when the specified circle is drawn. Assume that the entire circle will fit within the area of the display.

Sample Input

1 1 1
5 2 5
0 0 0

Sample Output

4
88
分析:1,根据对称性,只求左上角区域即可;2,从1到r-1,求出对应直角三角形的高,如果刚好为整数,就加上h,否则加上h + 1,最后再加上r,再乘以4。代码:
#include<iostream>
#include<cmath>
using namespace std;
int main(){
    cin.sync_with_stdio(false);
    long long x,y,r;
    while(cin >> x >> y >> r){
        if(x == 0 && y == 0 && r == 0)
            break;
        else{
            long long ans = r;
            for(long long i = 1;i <= r - 1;i++){
                long long h = (long long)sqrt((long double)r * r - i * i);
                if(h * h + i * i == r * r) ans += h;
                else ans += h + 1;
            }
            cout << 4 * ans << endl;
        }
    }
    return 0;
}
 
时间: 2024-10-27 11:03:29

csuoj-1011-Counting Pixels的相关文章

Learning OpenCV Lecture 3 (Counting the Pixels with Histograms)

In this chapter, we will cover: Computing the image histogram Applying look-up tables to modify image appearance Equalizing the image histogram Backprojecting a histogram to detect specific image content Using the mean shift algorithm to find an obje

leetCode 338. Counting Bits | Dynamic Programming | Medium

338. Counting Bits Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example:For num = 5 you should return [0,1,1,2,1,2]. 题目大意

UVA - 12075 Counting Triangles

Description Triangles are polygons with three sides and strictly positive area. Lattice triangles are the triangles all whose vertexes have integer coordinates. In this problem you have to find the number of lattice triangles in anMxN grid. For examp

POJ 2386 Lake Counting 搜索题解

简单的深度搜索就可以了,看见有人说什么使用并查集,那简直是大算法小用了. 因为可以深搜而不用回溯,故此效率就是O(N*M)了. 技巧就是增加一个标志P,每次搜索到池塘,即有W字母,那么就认为搜索到一个池塘了,P值为真. 搜索过的池塘不要重复搜索,故此,每次走过的池塘都改成其他字母,如'@',或者'#',随便一个都可以. 然后8个方向搜索. #include <stdio.h> #include <vector> #include <string.h> #include

Counting Divisors HDU - 6069

Counting Divisors HDU - 6069 题意:给定区间[a,b]和k,求xk有多少因子(x属于[a,b]),求和. 题解:http://blog.csdn.net/zlh_hhhh/article/details/76680641 a.b最大可达到1e12,但是b-a<1e6. 一开始愚蠢的一个一个分解然后去求有多少因子然后求和,范围那么大裸裸的超时啊! 可以枚举素数,对每一个素数,把区间内所有可以分解的进行分解. 最后再求和. 1 #include <bits/stdc++

UVa 11401 Triangle Counting (计数DP)

题意:给定一个数 n,从1-n这些数中任意挑出3个数,能组成三角形的数目. 析:dp[i] 表示从1-i 个中任意挑出3个数,能组成三角形的数目. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <

POJ 1011 - Sticks DFS+剪枝

POJ 1011 - Sticks 题意:    一把等长的木段被随机砍成 n 条小木条    已知他们各自的长度,问原来这些木段可能的最小长度是多少 分析:    1. 该长度必能被总长整除    2. 从大到小枚举,因为小长度更灵活, 可拼接可不拼接    3. 因为每一跟木条都要用到, 故若轮到其中一根原始木段选它的第一根木条时,若第一根木条若不满足,则显然第一根木条在接下来任何一根原始木段都不会满足,故无解    4. 由于所有棒子已排序,在DFS时,若某根棒子未被选,则跳过其后面所有与

Getting or Setting Pixels

Getting or Setting Pixels The safe (slow) way Suppose you are working on an Image<Bgr, Byte>. You can obtain the pixel on the y-th row and x-th column by calling Bgr color = img[y, x]; Setting the pixel on the y-th row and x-th column is also simple

leetcode笔记:Counting Bits

一. 题目描述 Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array. Example: For num = 5 you should return [0,1,1,2,1,2]. Follow up: It

LightOJ - 1148 Mad Counting(坑)

Mad Counting Time Limit: 500MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description Mob was hijacked by the mayor of the Town "TruthTown". Mayor wants Mob to count the total population of the town. Now the naive a