算法思想:将窗口信息按顺序存入vector(从底层到顶层),每次点击屏幕,从顶层到底层搜索点击到了哪个窗口。将该窗口从vector中删除再重新加入,相当于该窗口变成最顶层,其余窗口顺序没有改变。
主要/核心函数分析:void click(int x, int y, vector<window>& wins) 每次点击屏幕,从顶层到底层搜索点击到了哪个窗口。将该窗口从vector中删除再重新加入,相当于该窗口变成最顶层,其余窗口顺序没有改变。
测试数据:
3 4
0 0 4 4
1 1 5 5
2 2 6 6
1 1
0 0
4 4
0 5
运行结果:
2
1
1
IGNORED
1 #include<iostream> 2 #include<vector> 3 using namespace std; 4 5 struct window 6 { 7 int x1, x2, y1, y2;//坐标 8 int num;//序列号 9 window(int x1, int y1, int x2, int y2, int num) :x1(x1), y1(y1), x2(x2), y2(y2), num(num) {};//构造函数 10 }; 11 12 void click(int x, int y, vector<window>& wins) 13 { 14 for (int i = wins.size() - 1; i >= 0; i--) //从最顶层开始搜索 15 { 16 if (x >= wins[i].x1 && y >= wins[i].y1 && x <= wins[i].x2 && y <= wins[i].y2) 17 { 18 cout << wins[i].num << endl; 19 wins.push_back(wins[i]);//点击的窗口到最顶层 20 auto it = wins.begin() + i; 21 wins.erase(it);//清除原来的位置信息 22 return; 23 } 24 } 25 cout << "IGNORED" << endl; 26 return; 27 } 28 29 int main() 30 { 31 int N; 32 int M; 33 cin >> N >> M; 34 35 vector<window>wins;//记录窗口信息 36 37 for (int i = 1; i <= N; i++) 38 { 39 int x1, x2, y1, y2; 40 cin >> x1 >> y1 >> x2 >> y2; 41 window win(x1, y1, x2, y2, i); 42 wins.push_back(win);//有底层到最顶层 43 } 44 45 for (int j = 0; j < M; j++) 46 { 47 int x, y; 48 cin >> x >> y; 49 click(x, y, wins); 50 } 51 }
标签:y2,窗口,int,wins,点击,y1,x1 From: https://www.cnblogs.com/saucerdish/p/17930114.html