UVA - 1312 Cricket Field 构造

该博客主要讨论了如何解决UVA 1312问题,即在W×H的网格中找到没有树的最大正方形。作者指出错误的暴力枚举方法,并将其与UVA 1382问题进行类比,说明寻找正方形与矩形的方法相似,关键在于取矩形的最小边。此外,还提到了一种较慢的解决方案,即通过判断点是否在矩形内部来切割矩形。

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

题目大意:在一个W * H的网格中有n棵树,要求你在这个网格中找出最大个的一个正方形,这个正方形内部不能有树,边上可以有树

解题思路:刚开始以为要暴力枚举每一个点,结果发现错了,其实这题就像UVA - 1382 Distant Galaxy这题一样,只不过这个是要找正方形,找正方形和找矩形类似,只需要取矩形的最小边就可以了

#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 110

struct Point{
    int x, y;
}p[maxn];
int n, W, H, y[maxn];

bool cmp(const Point a, const Point b) {
    if(a.x == b.x)
        return a.y < b.y;
    return a.x < b.x;
}

void init() {
    scanf("%d%d%d",&n, &W, &H);
    for(int i = 0; i < n; i++) {
        scanf("%d%d", &p[i].x, &p[i].y);
        y[i] = p[i].y;
    }
    y[n] = 0, y[n + 1] = H;
    sort(y, y + n + 2);
    sort(p, p + n, cmp);
}

void solve() {
    int ans = 0, xx, yy;
    int m = unique(y, y + n + 2) - y;
    for(int i = 0; i < m; i++)
        for(int j = i + 1; j < m; j++) {
            int h = y[j] - y[i], pre = 0, w;
            for(int k = 0; k < n; k++) {
                if(p[k].y <= y[i] || p[k].y >= y[j])
                    continue; 
                w = p[k].x - pre;
                if(ans < min(w, h)) {
                    ans = min(w,h);
                    xx = pre;
                    yy = y[i];
                }
                pre = p[k].x;
            }
            w = W - pre;
            if(ans < min(h, w)) {
                ans = min(h,w);
                xx = pre;
                yy = y[i];
            }       
        }
    printf("%d %d %d\n",xx, yy, ans);
}

int main() {
    int test;
    scanf("%d", &test);
    while(test--) {
        init();
        solve();
        if(test)
            printf("\n");
    }
    return 0;
}

第二种解法:判断这个点是否在当前矩形内部,如果在当前矩形内部就将当前这个矩形切割成四个矩形,这种做法跑得比较慢

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 1000005
struct Rectangular{
    int x, y, w, h;
}Rec[maxn];

int main() {
    int test, n, W, H;
    scanf("%d", &test);
    while(test--) {
        scanf("%d%d%d", &n, &W, &H);
        Rec[0].x = Rec[0].y = 0;
        Rec[0].h = H, Rec[0].w = W;
        int cnt = 1;

        int w, h, x, y, Rx, Ry;
        for(int i = 0; i < n; i++) {
            scanf("%d%d", &x, &y);
            for(int j = 0; j < cnt; j++) {
                w = Rec[j].w;
                h = Rec[j].h;
                Rx = Rec[j].x;
                Ry = Rec[j].y;
                if(x > Rx && x < Rx + w && y > Ry && y < Ry + h) {

                    Rec[cnt].x = Rx;
                    Rec[cnt].y = Ry;
                    Rec[cnt].w = w;
                    Rec[cnt++].h = y - Ry;

                    Rec[cnt].x = Rx;
                    Rec[cnt].y = Ry;
                    Rec[cnt].w = x - Rx;
                    Rec[cnt++].h = h;

                    Rec[cnt].x = x;
                    Rec[cnt].y = Ry;
                    Rec[cnt].h = h;
                    Rec[cnt++].w = w + Rx - x;

                    Rec[cnt].x = Rx;
                    Rec[cnt].y = y;
                    Rec[cnt].w = w;
                    Rec[cnt++].h = Ry + h - y;

                    Rec[j].w = Rec[j].h = 0;
                }
            }
        }

        int ans = 0, mark = 0;
        for(int i = 0; i < cnt; i++) {  
            if(ans < min(Rec[i].h, Rec[i].w)) {
                ans = min(Rec[i].h, Rec[i].w);
                mark = i;
            }
        }
        printf("%d %d %d\n", Rec[mark].x, Rec[mark].y, ans);
        if(test)
            printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值