201403-2 窗口

实现


#include <cstdio>
#include <vector>
#include <algorithm>

class window {

public:  
    int x1,x2;
    int y1,y2;
    int id;

    window() {}
    window(int x1,int y1, int x2, int y2, int id) :
    x1(x1), y1(y1), x2(x2), y2(y2), id(id) {}

    bool inRange(int x, int y) {
        return (x1 <= x && x <= x2) && (y1 <= y && y <= y2);
    }

    int get_id() {
        return this->id;
    }

};

class Window_interaction {

public:
    std::vector<window> windows;

    int id;

    Window_interaction() {
        id = 1;
    }

    void add_windows (int x1, int y1, int x2, int y2) {
        window one_window = window(x1,y1,x2,y2,this->id);
        this->id++;
        windows.insert(windows.begin(),one_window);
    }

    int response_click(int x, int y) {
        int response_id = 0;
        window click_window;

        std::vector<window>::iterator ite = windows.begin();
        for ( ;ite != windows.end(); ++ite) {
            if (ite->inRange(x,y)) {
                response_id = ite->id;
                click_window.x1 = ite->x1;
                click_window.x2 = ite->x2;
                click_window.y1 = ite->y1;
                click_window.y2 = ite->y2;
                click_window.id = ite->id;
                windows.erase(ite);
                break;
            }
        }

        if (response_id) {
            windows.insert(windows.begin(),click_window);
        }
        
        return response_id;
    }

};

int main() {

    int windows_num, click_num;
    Window_interaction win_interact;
    scanf("%d%d",&windows_num, &click_num);

    for(int i = 0;i < windows_num;++i) {
        int x1,y1,x2,y2;
        scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
        win_interact.add_windows(x1,y1,x2,y2);
    }

    for (int i = 0;i < click_num;++i) {
        int x, y;
        scanf("%d%d",&x,&y);

        int response_id = win_interact.response_click(x,y);
        if (response_id) {
            printf("%d\n",response_id);
        } else {
            printf("IGNORED\n");
        }
    }


}


posted @ 2020-08-22 23:55  amonqsq  阅读(60)  评论(0编辑  收藏  举报