UVA10703 Free spots【暴力】

Write a program, that, given a board, and a list of rectangular sub-portions of the board, returns the number of positions that belong to no sub-portion.
Input
The input consists of a series of test sets separated by blank lines. A test set starts with a line with three numbers W, H and N, giving respectively the width, the height and the number of sub-boards. These values satisfy the following constraints: 1 ≤ W, H ≤ 500 and 0 ≤ N ≤ 99. Follow then N lines, composed of four integers X1, Y1, X2, Y2, such that (X1, Y1) and (X2, Y2) are the positions of two opposite corners of a sub-board. These values satisfy the following constraints: 1 ≤ X1, X2 ≤ W and 1 ≤ Y1, Y2 ≤ H. The end of the input is reached when the numbers W, H and N are equal to 0.
    This last line shall not be considered as a test set.
Output
The program shall output each result on a line by its own, following the format given in the sample output.
Sample Input
1 1 1
1 1 1 1
2 2 2
1 1 1 2
1 1 2 1
493 182 3
349 148 363 146
241 123 443 147
303 124 293 17
0 0 0
Sample Output
There is no empty spots.
There is one empty spot.
There are 83470 empty spots.

问题链接UVA10703 Free spots
问题简述:(略)
问题分析
    给定一个大矩形的长和宽,给定若干个小矩形(对角坐标),计算大矩形上去掉所有小矩形后还剩下多少点。
    这里采用的方法是标记后统计一下的方法,最为简单易懂。如果数据量巨大,也许需要用二维线段树来解决。
程序说明:(略)
参考链接:(略)
题记:(略)

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

/* UVA10703 Free spots */

#include <bits/stdc++.h>

using namespace std; 

const int N = 500;
bool flag[N + 1][N + 1];

int main()
{
    int w, h, n, x1, y1, x2, y2;

    while (~scanf("%d%d%d", &w, &h, &n) && (w || h || n)) {
        memset(flag, true, sizeof(flag));

        for(int i = 0; i < n ; i++) {
            scanf("%d%d%d%d", &x1, &y1, &x2, &y2);

            if(x1 > x2) swap(x1, x2);
            if(y1 > y2) swap(y1, y2);
            for (int j = x1; j <= x2 ; j++)
                for (int k = y1; k <= y2 ; k++)
                    flag[j][k] = false;
        }

        int ans = 0;
        for (int i = 1; i <= w ; i++)
            for (int j = 1; j <= h ; j++)
            if(flag[i][j])
                ans ++;

        if (ans == 0)
            printf("There is no empty spots.\n");
        else if (ans == 1)
            printf("There is one empty spot.\n");
        else
            printf("There are %d empty spots.\n",ans);
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值