Uva oj 142-mouse clicks (click the mouse)

Source: Internet
Author: User
Problem
Problem

A typical indexing Wing System on a computer will provide a number of icons on the screen as well as some defined regions. when the mouse button is clicked, the system has to determine where the cursor is and what is being selected. for this problem we assume that a mouse click in (or on the border of) a region selects that region, otherwise it selects the closest visible icon (or icons in the case of a tie ).

Generally, computer Windows systems place many icons and predefined areas on the screen. When the mouse key is pressed, the system detects the position of the mouse and determines what is clicked. In this case, we think that if you click an area (point on the edge), We will select the area. Otherwise, we will select the nearest visible icon (there may be multiple ).

Consider the following screen:
Observe the following screen:

 

 

a mouse click at 'A' will select region. A mouse click at 'B' will select icon 1. A mouse click at 'C' will select icons 6 and 7. A mouse click at 'D' is ambiguous. the ambiguity is resolved by assuming that one region is in front of another. in the data files, later regions can be assumed to be in front of earlier regions. since regions are labeled in order of appearance (see later) 'd Select C. note that regions always overlap icons so that obscured icons need not be considered and that the origin (0, 0) is at the top left corner.
place the cursor at "a" and select area A. Point at "B", select icon 1, in "C", icons 6 and 7 are selected, while in 'D', a difference occurs. Select a region that is located at the top of the page. In the input data, the given area should be considered before the given area. Note that the area will overwrite all the icons in the corresponding position, and the covered graph should be ignored. The coordinate origin (0, 0) is located in the lower left corner.

Write a program that will read in a series of region and Icon definitions followed by a series of mouse clicks and return the selected items. coordinates will be given as pairs of integers in the range 0 .. 499 and you can assume that all icons and regions lie wholly within the screen. your program must number all icons (even invisible ones) in the order of arrival starting from 1 and label regions alphabetically in the order of arrival starting from 'A '.
Write oneProgramRead the definition of a group of regions and icons, and the position of a group of mouse clicks, and return the selected object. Each coordinate is two integers ranging from 0 to 499. You can think that all icons and areas are not on the outside of the screen. Your program should numbered the icon from 1 in the input order and numbered the area from a in the alphabetic order.

 

Input
Input

Input will consist of a series of lines. each line will identify the type of data: I for icon, R for Region and M for mouse click. there will be no separation between the specification part and the event part, however no icon or region specifications will follow the first mouse click. an I will be followed by the coordinates of the center of the icon, R will be followed by the coordinates of the top left and bottom right corners respectively and m will be followed by the coordinates of the cursor at the time of the click. there will always be at least one visible icon and never more than 25 regions and 50 icons. the entire file will be terminated by a line consisting of a single #.
The input consists of multiple rows. The data type is marked at the beginning of each line: I is the icon, r is the area, and m is the mouse click position. There is no interval between the input of the data definition part and the input of the Click Event part. However, after you start to input the mouse data, the icon or area data will no longer appear. Mark I is followed by the coordinates of the icon center. Mark R is followed by the coordinates of the upper left corner and the lower right corner. Mark m is followed by the coordinates of the pointer position when the mouse clicks. There must be at least one visible icon, a maximum of 25 areas and 50 icons. All input data is ended by a single character # Of an exclusive row.

 

Output
Output

Output will consist of one line for each mouse click, containing the selection (s) for that click. regions will be identified by their single character identifier, icon numbers will be written out right justified in a field of width 3, and where there is more than one icon number they will appear in Increasing numerical order.
Outputs the selected objects in a row for each mouse click event. The area is represented by a single letter. The icon should be right aligned and output in the format of 3 in width. If more than one icon is selected, it should be output in ascending order of numbers.

 

Sample Input
Input example

I 216 28
R 22 19 170 102
I 40 150
I 96 138
I 36 193
R 305 13 425 103
I 191 184
I 387 200
R 266 63 370 140
I 419 134
I 170 102
M 50 50
M 236 30
M 403 167
M 330 83
#

 

Sample output
Output example

A
1
6 7
C

 

Analysis
Analysis

This question is not complex.Algorithm, Determining the point inside the rectangle and calculating the distance are the most basic operations, simply by the question. Note: The entered rectangle and icon cannot be changed or deleted at will; otherwise, the correct number cannot be obtained during the output; Do not forget to ignore the icon overwritten by the rectangle.

 

Solution
Answer
# Include <iomanip> # include <iostream> # include <iterator> # include <deque> using namespace STD; struct point {int X; int y ;}; struct rect {point TL; point BR ;}; // determine whether the point is in the bool pointinrect (const point & PT, const rect & rc) {return (pt. x> = RC. TL. X & PT. x <= rc.br. X & PT. y> = RC. TL. Y & PT. Y <= rc.br. y);} // calculate the square int pointdistance (const point & pt1, const point & pt2) of the distance between two points {point VEC = {pt2.x-pt1.x, pt2.y- Pt1.y}; Return (VEC. x * Vec. X + Vec. y * Vec. y);} // main function int main (void) {deque <point> icons, clicks; deque <rect> rects; // read each group of data cyclically for (char tag = 0; CIN >>tag & tag! = '#';) {Rect RC; Switch (TAG) {Case 'I': // enter the icon data CIN> RC. TL. x> RC. TL. y; icons. push_back (RC. TL); break; Case 'r': // input rectangular data CIN> RC. TL. x> RC. TL. y> rc.br. x> rc.br. y; If (rc.br. x <RC. TL. x) * (int *) 0 = 0; If (rc.br. Y <RC. TL. y) * (int *) 0 = 0; rects. push_back (RC); If (rects. size ()> 26) * (int *) 0 = 0; break; Case 'M': // enter the mouse click event (coordinates) CIN> RC. TL. x> RC. TL. y; clicks. push_back (RC. TL); break ;}} deque <point> :: Iterator iicon, iclick; deque <rect>: iterator irect; // check whether an icon exists and is overwritten by a rectangle for (iicon = icons. Begin (); iicon! = Icons. End (); ++ iicon) {for (irect = rects. Begin (); irect! = Rects. end (); ++ irect) {If (pointinrect (* iicon, * irect) {iicon-> X = 10000; // move the icon to infinity, ignore iicon-> Y = 10000; // It cannot be deleted, so as not to affect the icon number }}// process each mouse click event for (iclick = clicks. begin (); iclick! = Clicks. end (); ++ iclick) {// according to the question requirement, check whether the position is in the deque <rect >:: reverse_iterator rirect; For (rirect = rects. rbegin (); rirect! = Rects. rend ()&&! Pointinrect (* iclick, * rirect); ++ rirect); // If a vertex is found in a rectangle, output this rectangle if (rirect! = Rects. rend () {cout <(char) (distance (rirect, rects. rend ()-1 + 'A') <Endl; continue;} // find the icon closest to the mouse position, save to the dynamic array selicons deque <int> selicons (1, 1); deque <point >:: iterator inear = icons. begin (), iicon; int nnear = pointdistance (* iclick, * inear); For (iicon = inear + 1; iicon! = Icons. end (); ++ iicon) {int ndist = pointdistance (* iclick, * iicon); // calculate the distance from if (ndist <nnear) {inear = iicon; nnear = ndist; selicons. clear (); // clear the original array and replace selicons with a new icon. push_back (distance (icons. begin (), iicon) + 1);} else if (ndist = nnear) {// Add the array selicons when an icon with the shortest distance is found. push_back (distance (icons. begin (), iicon) + 1) ;}// Number of the output icon in the required format: deque <int >:: iterator ISEL; For (ISEL = selicons. begin (); ISEL! = Selicons. End (); ++ ISEL) {cout <SETW (3) <* ISEL;} cout <Endl;} return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.