问题描述
试题编号: | 201403-2 |
试题名称: | 窗口 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 在某图形操作系统中,有 N 个窗口,每个窗口都是一个两边与坐标轴分别平行的矩形区域。窗口的边界上的点也属于该窗口。窗口之间有层次的区别,在多于一个窗口重叠的区域里,只会显示位于顶层的窗口里的内容。 输入格式 输入的第一行有两个正整数,即 N 和 M。(1 ≤ N ≤ 10,1 ≤ M ≤ 10) 输出格式 输出包括 M 行,每一行表示一次鼠标点击的结果。如果该次鼠标点击选择了一个窗口,则输出这个窗口的编号(窗口按照输入中的顺序从 1 编号到 N);如果没有,则输出"IGNORED"(不含双引号)。 样例输入 3 4 样例输出 2 样例说明 第一次点击的位置同时属于第 1 和第 2 个窗口,但是由于第 2 个窗口在上面,它被选择并且被置于顶层。 |
思路:直接用STL里的list,没想到在删除的时候出了问题,直接用数组模拟了
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <list>
using namespace std;
struct node
{
int num;
int x1;
int x2;
int y1;
int y2;
}a[15];
int main()
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
a[i].num = i;
scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
}
node b;
int x,y;
for(int i=1; i<=m; i++)
{
scanf("%d%d",&x,&y);
int j;
for(j=n; j>=1; j--)
{
if(x >= a[j].x1 && x <= a[j].x2 && y >= a[j].y1 && y <= a[j].y2)
{
b = a[j];
printf("%d\n",a[j].num);//应该是先输出,不然后面的次序改了
for(int k=j; k<n; k++)
{
a[k] = a[k+1];
}
a[n] = b;
break;
}
}
if(j == 0)
printf("IGNORED\n");
}
return 0;
}
错误代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <list>
using namespace std;
struct node
{
int num;
int x1;
int x2;
int y1;
int y2;
}a[15];
int main()
{
int n,m;
list<node> l;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
{
a[i].num = i;
scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&a[i].x2,&a[i].y2);
l.push_back(a[i]);
}
for(int i=1; i<=m; i++)
{
bool flag = false;
int x,y;
scanf("%d%d",&x,&y);
list<node>::reverse_iterator rit;
for(rit=l.rbegin(); rit!= l.rend(); rit++)
{
if(x >= rit->x1 && x <= rit->x2 && y >= rit->y1 && y <= rit->y2)
{
flag = true;
cout<<rit->num<<endl;
l.push_back(*rit);
l.erase(rit);//删除这个元素,死活报错,用数组移
break;
}
}
if(!flag)
cout<<"IGNORED"<<endl;
}
return 0;
}