首页 > ACM题库 > HDU-杭电 > HDU 3332-Windows[解题报告]HOJ
2014
03-16

HDU 3332-Windows[解题报告]HOJ

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 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 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.

输出:

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.

样例输入:

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

样例输出:

Desktop 1:
window 3
window 1
background
window 2
Desktop 2:
background
window 2

刚开始以为是个水题,跟以前一到省赛题很像。但发现第二个样例好像对不上。。。

关键在于pixels(像素)这个单词上,以前的题目是一点一点的算,这道题是按一格一格来划分区域的,这样理解就可以对上了。

 

#include<iostream>
using namespace std;

int cnt=1;

struct jsj
{
    int x1,y1;
    int x2,y2;
}a[101];

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0) break;
        for(int i=0;i<n;i++)
        {
            int c,d;
            scanf("%d%d%d%d",&a[i].x1,&a[i].y1,&c,&d);
            a[i].x2=a[i].x1+d-1;
            a[i].y2=a[i].y1+c-1;
        }

        int m;
        scanf("%d",&m);
        printf("Desktop %d:/n",cnt++);
        for(int i=0;i<m;i++)
        {
            bool f=false;
            int q1,q2;
            scanf("%d%d",&q1,&q2);
            for(int j=n-1;j>=0;j--)
            {
                if(q1>=a[j].x1 && q1<=a[j].x2 && q2>=a[j].y1 && q2<=a[j].y2)
                {
                    f=true;
                    printf("window %d/n",j+1);
                    break;
                }
            }
            if(!f) printf("background/n");
        }
    }
    return 0;
}

参考:http://blog.csdn.net/jsjdream/article/details/5595334


  1. 给你一组数据吧:29 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 10 1000。此时的数据量还是很小的,耗时却不短。这种方法确实可以,当然或许还有其他的优化方案,但是优化只能针对某些数据,不太可能在所有情况下都能在可接受的时间内求解出答案。