HDU - 1264 Counting Squares

本文详细解析了一种通过矩形覆盖来计算单位正方形数量的算法。输入为一系列指定矩形角落坐标的行,输出为所有矩形覆盖的单位正方形总数。文章提供了一个C++实现示例,采用暴力法解决小规模数据集问题,关键在于避免重复计算被多个矩形覆盖的区域。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Your input is a series of rectangles, one per line. Each rectangle is specified as two points(X,Y) that specify the opposite corners of a rectangle. All coordinates will be integers in the range 0 to 100. For example, the line
5 8 7 10
specifies the rectangle who's corners are(5,8),(7,8),(7,10),(5,10).
If drawn on graph paper, that rectangle would cover four squares. Your job is to count the number of unit(i.e.,1*1) squares that are covered by any one of the rectangles given as input. Any square covered by more than one rectangle should only be counted once.

Input

The input format is a series of lines, each containing 4 integers. Four -1's are used to separate problems, and four -2's are used to end the last problem. Otherwise, the numbers are the x-ycoordinates of two points that are opposite corners of a rectangle.

Output

Your output should be the number of squares covered by each set of rectangles. Each number should be printed on a separate line.

Sample Input

5 8 7 10
6 9 7 8
6 8 8 11
-1 -1 -1 -1
0 0 100 100
50 75 12 90
39 42 57 73
-2 -2 -2 -2

Sample Output

8
10000

这道题本来应该是用线段树扫描线做的,但是看到数据量只有100*100,直接暴力就可以,细节还是很重要的

#include<bits/stdc++.h>
using namespace std;
bool vis[105][105];//判断一个点是否已经被覆盖了
int main()
{
    while(1)
    {
        memset(vis,false,sizeof(vis));
        int x1,x2,y1,y2;
        int flag=1;
        while(cin>>x1>>y1>>x2>>y2)
        {
            if(x1==-1&&x2==-1&&y1==-1&&y2==-1)
            {
                break;
            }
            if(x1==-2&&x2==-2&&y1==-2&&y2==-2)
            {
                flag=0;
                break;
            }
            for(int i=min(x1,x2);i<max(x1,x2);i++)//注意此地细节,因为对点横纵坐标大小关系不一样,所以要这样
            {
                for(int j=min(y1,y2);j<max(y1,y2);j++)
                {
                    vis[i][j]=true;
                }
            }
        }
        int sum=0;
        for(int i=0;i<=100;i++)
        {
            for(int j=0;j<=100;j++)
            {
                if(vis[i][j])
                {
                    sum++;//判断有多少个小的1*1的方格在里面,加起来就是面积
                }
            }
        }
        cout<<sum<<endl;
        if(flag==0)
        {
            break;
        }
    }
    return 0;
}

 

Robert is a famous engineer. One day he was given a task by his boss. The background of the task was the following: Given a map consisting of square blocks. There were three kinds of blocks: Wall, Grass, and Empty. His boss wanted to place as many robots as possible in the map. Each robot held a laser weapon which could shoot to four directions (north, east, south, west) simultaneously. A robot had to stay at the block where it was initially placed all the time and to keep firing all the time. The laser beams certainly could pass the grid of Grass, but could not pass the grid of Wall. A robot could only be placed in an Empty block. Surely the boss would not want to see one robot hurting another. In other words, two robots must not be placed in one line (horizontally or vertically) unless there is a Wall between them. Now that you are such a smart programmer and one of Robert's best friends, He is asking you to help him solving this problem. That is, given the description of a map, compute the maximum number of robots that can be placed in the map. Input The first line contains an integer T (<= 11) which is the number of test cases. For each test case, the first line contains two integers m and n (1<= m, n <=50) which are the row and column sizes of the map. Then m lines follow, each contains n characters of '#', '', or 'o' which represent Wall, Grass, and Empty, respectively. Output For each test case, first output the case number in one line, in the format: "Case :id" where id is the test case number, counting from 1. In the second line just output the maximum number of robots that can be placed in that map.
06-06
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值