解题思路:
1,由题意可知模拟窗口顺序是解决问题的关键,并且每次点击完后需要对窗口顺序进行调整,我使用的是一个简单的链表(顺便练习一下简单的链表操作),采用尾插法刚刚可以表示窗口的层数位置,并且我觉得这样的话调整窗口顺序时比较方便,但由于窗口顺序的设置当时没完全读懂(理解水平有限),所以花了些时间去想。
测试代码:
#include<iostream>
#include<stdlib.h>
#include<vector>
using namespace std;
typedef struct list{
int index;
int x1, y1, x2, y2;
struct list *next;
}*Crd,crd;
void freeMem(Crd Head) {//简单递归
if (Head != NULL) {
freeMem(Head->next);
}
free(Head);
}
int main() {
int N, M;//含义与题同
Crd Head = (Crd)malloc(sizeof(crd));
Head->index = 0;
Head->next = NULL;
Head->x1 = Head->x2 = Head->y1 = Head->y2 = -1;
cin >> N >> M;
for (int i = 0; i < N; ++i) {
Crd tmp = (Crd)malloc(sizeof(crd));
cin >> tmp->x1 >> tmp->y1 >> tmp->x2 >> tmp->y2;
tmp->index = i+1;//表示层数
//头插法
tmp->next = Head->next;
Head->next = tmp;
}
vector<int> res;//存入负数时,表示输出IGNORED
for (int i = 0; i < M; ++i) {
int cx, cy;
cin >> cx >> cy;
int count = 0;
//直接处理将结果存入一个vector
Crd temp = Head->next;
while (temp != NULL) {
if (cx >= temp->x1&&cx <= temp->x2&&cy >= temp->y1&&cy <= temp->y2) {
res.push_back(temp->index);
//调整窗口顺序
Crd Jude = Head;
while (Jude->next != temp ) {
Jude = Jude->next;
}
if (Jude->next == temp && Head->next != temp) {
Jude->next = temp->next;
temp->next = Head->next;
Head->next = temp;
}
break;
}
temp = temp->next;
count++;
}
if (N == count) {
res.push_back(-1);
}
}
for (int i = 0; i < res.size(); ++i) {
if (-1 == res.at(i)) {
cout << "IGNORED" << endl;
}
else {
cout << res.at(i) << endl;
}
}
freeMem(Head);//释放malloc获取的空间
system("pause");
return 0;
}
欢迎评论,共同进步。