CCF画图

#include <stdio.h>

struct rectStruct{
    int a,b;
    int m, n;
};
struct rectStruct rects[100];
int rectS = 0;
int n;

int inYLine(int a, int ID);
int inXLine(int a, int ID);
int inRect(int x, int y, int ID);
void coincidentS(int a, int b, int m, int n, int ID);
void computeS();
void runRect(int ID);
int main(void) {
    // 将矩形
    // 遍历每一个矩形
    // 然后遍历每一个矩形中的每一个小方块
    // 判断这个小方块 属不属于其他矩形
    int i = 0;
    int s = 0;
    // printf("%d\n", n);
    scanf("%d", &n);
    // printf("输出数目1:%d\n", n);
    //将数据输入
    for(i = 0; i < n; i++) {
        scanf("%d %d %d %d", &rects[i].a, &rects[i].b, &rects[i].m, &rects[i].n);
    }

    computeS();
    return 0;
}

void computeS() {
    // 遍历每一个矩形
    int i = 0;
    int s = 0;

    for(i = 0; i < n; i++) {
        // 遍历矩形的内部
        // printf("第三个有没有输出n:%d\n", i);
        runRect(i);
    }

    printf("%d", rectS);
}
void runRect(int ID) {
    // 看矩形里面的小矩形, 被几个矩形所占有
    // 先得到这个小矩形
    // 比如(1, 1)和(4, 4)
    // 第一个小矩形是(1, 1) (2, 2)/ (2, 1) (3, 2)
    int i, j;
    int startW = rects[ID].a;
    int finalW = rects[ID].m - 1;
    int startH = rects[ID].b;
    int finalH = rects[ID].n - 1;
    for(j = startH; j <= finalH; j++) {
        for(i = startW; i <= finalW; i++) {
            coincidentS(i, j, i+1, j+1, ID);
        }
    }
}
void coincidentS(int a, int b, int x, int y, int ID) {
    // 只检查ID之前的,
    int i;
    for(i = ID-1; i > -1; i--) {
        // printf("Output ID :%d\n", i);
        if(inRect(a, b, i) && inRect(x, y, i)) {
            break;
        }
    }
    if(i <= -1) {
        rectS++;
    }
}
int inRect(int x, int y, int ID) {
    return inXLine(x, ID) && inYLine(y, ID);
}
int inXLine(int a, int ID) {
    int in = (a - rects[ID].a) * (a - rects[ID].m);
    return in <= 0;
}
int inYLine(int a, int ID) {
    int in = (a - rects[ID].b) * (a - rects[ID].n);
    return in <= 0;
}

写着写着, 好像就想起来萧大强调的思想, 写程序是从上往下写的, 所以当我这个程序判断, 这个小矩形是不是在之前的矩形的时候, 就看左下角的点, 和又下角的点在不在.(这是一个函数吧), 之后看这个点在不在, 就看x在不在y在不在(这又是一个函数).这些函数, 还可以单独拿出来测试, 也不会影响别的代码.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值