HDU 3332 Windows

Emma is not very tidy with the desktop of her computer. She has the habit of opening windows on the screen and then not closing the application that created them. The result, of course, is a very cluttered desktop with some windows just peeking out from behind others and some completely hidden. Given that Emma doesn’t log off for days, this is a formidable mess. Your job is to determine which window (if any) gets selected when Emma clicks on a certain position of the screen. 

Emma’s screen has a resolution of 10  6 by 10  6. When each window opens its position is given by the upper-left-hand corner, its width, and its height. (Assume position (0,0) is the location of the pixel in the upper-left-hand corner of her desktop. So, the lower-right-hand pixel has location (999999, 999999).)
Input
Input for each test case is a sequence of desktop descriptions. Each description consists of a line containing a positive integer  n, the number of windows, followed by  n lines,  n ≤ 100, describing windows in the order in which Emma opened them, followed by a line containing a positive integer  m, the number of queries, followed by  m lines, each describing a query. Each of the  n window description lines contains four integers  r, c, w, and  h, where (  r, c) is the row and column of the upper left pixel of the window, 0 ≤  r, c ≤ 999999, and  w and  h are the width and height of the window, in pixels, 1 ≤ w, h. All windows will lie entirely on the desktop (i.e., no cropping). Each of the  m query description lines contains two integers  cr and  cc, the row and column number of the location (which will be on the desktop). The last test case is followed by a line containing 0.
Output
Using the format shown in the sample, for each test case, print the desktop number, beginning with 1, followed by  m lines, one per query. The  i-th line should say either “window  k”, where  k is the number of the window clicked on, or “background” if the query hit none of the windows. We assume that windows are numbered consecutively in the order in which Emma opened them, beginning with 1. Note that querying a window does not bring that window to the foreground on the screen.
Sample Input
3
1 2 3 3
2 3 2 2
3 4 2 2
4
3 5
1 2
4 2
3 3
2
5 10 2 10
100 100 100 100
2
5 13
100 101
0
Sample Output
Desktop 1:
window 3
window 1
background
window 2
Desktop 2:
background
window 2

pixel是像素的意思,是块不是一个点。但可以转换成点。


这就简单了,我首先想到的是用map,但超时了,后来用链表做的,注意释放内存,不然会答案错误。最简单的方法是用线性表,因为题目里说了n<=100,也就是说不会超过100个数据。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<time.h>
using namespace std;
struct Node
{
    int x,y;
    int x1,y1;
    int g;
    Node*next;
};
int main()
{
    int n,m;
    int x,y,w,h;

    Node* p;

    int cnt=0;
    while(~scanf("%d",&n)&&n)
    {
        //clock_t beg=clock();
        cnt++;
        Node* L=(Node*)malloc(sizeof(Node));
        L->next=NULL;
        for(int i=1;i<=n;i++)
        {
            scanf("%d%d%d%d",&x,&y,&w,&h);
            Node*s=(Node*)malloc(sizeof(Node));
            s->g=i;
            s->x1=x+h-1;
            s->y1=y+w-1;
            s->x=x;
            s->y=y;
            s->next=L->next;
            L->next=s;
        }
        scanf("%d",&m);
        int flag=0;
        printf("Desktop %d:\n",cnt);
        while(m--)
        {
            scanf("%d%d",&x,&y);
            p=L->next;
            flag=0;
            while(p!=NULL)
            {
                //printf("*******%d\n",p->g);
                if(p->x<=x&&p->x1>=x&&p->y<=y&&p->y1>=y)
                {
                    printf("window %d\n",p->g);
                    flag=1;
                    break;
                }
                 p=p->next;

            }
            if(flag==0)
            {
                printf("background\n");
            }
        }
        Node *pre=L;
        p=L->next;
        while(p!=NULL)
        {
            free(pre);
            pre=p;
            p=p->next;
        }
        free(pre);
        //clock_t end=clock();
        //cout<<"runtime="<<end-beg<<endl;
    }


    return 0;
}


Description Emma is not very tidy with the desktop of her computer. She has the habit of opening windows on the screen and then not closing the application that created them. The result, of course, is a very cluttered desktop with some windows just peeking out from behind others and some completely hidden. Given that Emma doesn’t log off for days, this is a formidable mess. Your job is to determine which window (if any) gets selected when Emma clicks on a certain position of the screen. Emma’s screen has a resolution of 106 by 106. When each window opens its position is given by the upper-left-hand corner, its width, and its height. (Assume position (0,0) is the location of the pixel in the upper-left-hand corner of her desktop. So, the lower-right-hand pixel has location (999999, 999999).) Input Input for each test case is a sequence of desktop descriptions. Each description consists of a line containing a positive integer n, the number of windows, followed by n lines, n ≤ 100, describing windows in the order in which Emma opened them, followed by a line containing a positive integer m, the number of queries, followed by m lines, each describing a query. Each of the n window description lines contains four integers r, c, w, and h, where (r, c) is the row and column of the upper left pixel of the window, 0 ≤ r, c ≤ 999999, and w and h are the width and height of the window, in pixels, 1 ≤ w, h. All windows will lie entirely on the desktop (i.e., no cropping). Each of the m query description lines contains two integers cr and cc, the row and column number of the location (which will be on the desktop). The last test case is followed by a line containing 0. Output Using the format shown in the sample, for each test case, print the desktop number, beginning with 1, followed by m lines, one per query. The i-th line should say either “window k”, where k is the number of the window clicked on, or “background” if the query hit none of the windows. We assume that windows are numbered consecutively in the order in which Emma opened them, beginning with 1. Note that querying a window does not bring that window to the foreground on the screen. Sample Input 3 1 2 3 3 2 3 2 2 3 4 2 2 4 3 5 1 2 4 2 3 3 2 5 10 2 10 100 100 100 100 2 5 13 100 101 0 Sample Output Desktop 1: window 3 window 1 background window 2 Desktop 2: background window 2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值