CCF-CSP 201403-2 window

Problem description
  In a graphics operating system, there are N windows, and each window is a rectangular area with two sides parallel to the coordinate axis. Points on the boundary of the window also belong to the window. There is a hierarchical difference between windows. In an area where more than one window overlaps, only the content in the window at the top level is displayed.
  When you click on a point on the screen, you select the top-most window in the clicked position, and this window will be moved to the top-level of all windows, and the remaining windows are in the same order. If the location you click does not belong to any window, the system will ignore your click.
  Now we want you to write a program to simulate the process of clicking the window.
Input format
  The first line of input has two positive integers, namely N and M. (1 ≤ N ≤ 10, 1 ≤ M ≤ 10) The
  next N lines give the positions of N windows in order from the bottom to the top. Each line contains four non-negative integers x1, y1, x2, y2, indicating that the coordinates of a pair of vertices of the window are (x1, y1) and (x2, y2). Ensure that x1 <x2, y1 2.
  The next M lines each contain two non-negative integers x, y, which represent the coordinates of a mouse click.
  The x and y coordinates of all points and rectangle vertices involved in the question do not exceed 2559 and 1439, respectively.
Output format
  output includes M lines, each line represents the result of a mouse click. If a window is selected by the mouse click, the window number is output (the windows are numbered from 1 to N in the order in which they are entered); if not, "IGNORED" (without double quotes) is output.
Sample input
3 4
0 0 4 4
1 1 5 5
2 2 6 6
1 1
0 0
4 4
0 5
Sample output
2
1
1
IGNORED
sample description
  The location of the first click belongs to both the first and second windows, but since the second window is on the top, it is selected and placed on the top layer.
  The position of the second click only belongs to the first window, so this click selects this window and puts it on the top level. The current hierarchical relationship of the three windows is exactly the opposite of the initial state.
  The position of the third click belongs to the scope of three windows at the same time, but since the first window is now at the top, it is selected.
  The last click (0, 5) does not belong to any window.

Summary of experience:
Use structure, which stores x1, y1, x2, y2 and window number.
Use vector to store windows one by one. The effect appears in the simulation window. The last element of the vector is the top-level window.
When clicked, traverse from the last element of the vector to determine whether the clicked coordinate is inside this element. If it is, output the window number of this element, then add this element after the last element, and delete the current window element (because it has become the top-level window).

C ++ code:

#include<bits/stdc++.h>
using namespace std;
struct node{
	int x1,y1,x2,y2;
	int rank;
	node(int x1,int y1,int x2,int y2,int rank):x1(x1),y1(y1),x2(x2),y2(y2),rank(rank){}
};
int main() {
	int n,m;
	cin>>n>>m;
	vector<node> v;
	for(int i=1;i<=n;i++){
		int a,b,c,d;
		cin>>a>>b>>c>>d;
		v.push_back(node(a,b,c,d,i));
	}
	while(m--){
		int x,y;
		bool flag = false;
		cin>>x>>y;
		for(auto it = v.end()-1;it>=v.begin();it--){
			if(x>=it->x1&&x<=it->x2&&y>=it->y1&&y<=it->y2){ //判断点击坐标是否在此窗口内
				cout<<it->rank<<endl;
				flag = true;
				node top = *it;
				v.erase(it);
				v.push_back(top);
				break;
			}
		}
		if(!flag) cout<<"IGNORED"<<endl;
	}
	return 0;
}
Published 111 original articles · won praise 2 · Views 3533

Guess you like

Origin blog.csdn.net/m0_38088647/article/details/100524341