题目描述
算法设计+题目分析
分析题目:
1、读入所有的窗口,并给所有的窗口设置一个级别,越后读入的窗口级别越高;
2、对所有窗口按照级别排序,然后找到(级别最高)&&(符合点击条件)的窗口(未找到输出IGNORED),重新设置这个窗口的级别;
3、对操作2循环m次,结束。
注意点+记录点
无
代码(90分)
环境
Visual C++6.0
#include <stdio.h>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;
int top;
struct window{
int x1,x2,y1,y2;
int top;//级别,级别越高越接近顶层
int index;//窗口标号
window(){}
window(int x_1, int y_1, int x_2, int y_2, int t, int d):x1(x_1), x2(x_2), y1(y_1), y2(y_2), top(t), index(d){}
}W[11];
bool cmp(window a, window b){
return a.top>b.top;
}
int main(){
//freopen("C:\\input.txt", "r", stdin);
int n,m;//窗口数、操作数
scanf("%d %d", &n, &m);
top=m-1;
for (int i=0; i<n; i++){//读入所有的窗口
int xx1,yy1,xx2,yy2;
scanf("%d %d %d %d", &xx1, &yy1, &xx2, &yy2);
W[i]=window(xx1,yy1,xx2,yy2, i,i+1);//初始化窗口级别
}
string s="IGNORED";
sort(W,W+n,cmp);//按照窗口级别排序
for (int j=0; j<m; j++){
int x,y;
scanf("%d %d", &x, &y);
for(int z=0; z<n; z++){//找到窗口级别最高的,且符合条件的窗口
if (x>=W[z].x1 && x<=W[z].x2 && y>=W[z].y1 && y<=W[z].y2){
//cout<<W[z].index<<endl;
if (j<m-1) printf("%d\n", W[z].index);
else printf("%d", W[z].index);
W[z].top=top+1;//设当前窗口的窗口级别为最高
top=top+1;
break;
}
if (z==n-1 && j<m-1) cout<<s<<endl;//找不到窗口
if (z==n-1 && j==m-1) cout<<s;
}
sort(W,W+n,cmp);//重新按照窗口级别排序,即把当前点击过的窗口放到最前面
}
return 0;
}
感谢链接
无