[Solution] annoying problem slideshow

Title Description

  Professor Lee will be held this afternoon to make a very important speech. Unfortunately, he was not a very tidy person, to use his own lecture slides casually piled together. Therefore, before the lecture he had to sort out these slides. As a scholar, efficiency, he hopes to complete it as simple as possible.

  Prof. use this speech altogether n slides (n≤26), the n-slide presentation to be used in the order have been numbered 1-n digital. Because the slide is transparent, so all of a sudden we can not see each number corresponding to the slide.

  Now we use capital letters A, B, C ...... again slides are numbered. Your task is to write a program to slide numbered and lettered correspondence, apparently this correspondence should be unique; if circumstances arise or some multiple corresponding numbered and lettered correspondence is not up, we He said correspondence can not be achieved.

 

Input Format

  The first line is only one integer n, n expressed slides;

  The next n lines each include four integer xmin, xmax, ymin, ymax (separated by spaces between integer) coordinates of the slides, which slide n the order they appear in the file are numbered from front to back as a, B, C, ......, and then the next n rows of n sequentially numbered coordinates x, y, outside of the slide is apparently not numbered.

 

Output Format

  If the correspondence can be realized, the output file should comprise n rows, each act a letter and a number, separated by a space, and each row in ascending order of the letter, note letters capitalized and output fixed; the other hand, if the corresponding It can not be achieved, in the first line of the file to the top grid output None. The first end of the row without extra spaces.

 

SAMPLE INPUT

4

6 22 10 20

4 18 6 16

8 20 2 18

10 24 4 8

9 15

19 17

11 7

21 11

 

Sample Output

A 4

B 1

C 2

D 3

 

answer

  According meaning of the questions we tend to think: We first determine which numbers which can match the slide, slide and then find the $ i $ $ j $ only numbers to match, and then let the numbers do not match the slide $ j $ $ k $, then Keep looking to meet slide $ i $ the $ j $ only numbers to match the $ i $, repeatedly looking for. The last judgment is not all the slides have been to find out too.

  Saying, is not that a topological sorting it. . .

#include <iostream>
#include <queue>

#define MAX_N (26 + 5)

using namespace std;

int n;
int xl[MAX_N], xr[MAX_N], yl[MAX_N], yr[MAX_N];
bool e[MAX_N][MAX_N];
int c[MAX_N];
queue <int> q;
int tot;

int main ()
{
    cin >> n;
    for(register int i = 1; i <= n; ++i)
    {
        cin >> xl[i] >> xr[i] >> yl[i] >> yr[i];
    }
    int x, y;
    for(register int i = 1; i <= n; ++i)
    {
        cin >> x >> y;
        for(register int j = 1; j <= n; ++j)
        {
            if(x < xl[j] || x > xr[j] || y < yl[j] || y > yr[j]) continue;
            e[j][i] = true;
            ++c[j];
        }
    }
    for(register int i = 1; i <= n; ++i)
    {
        if(c[i] == 1) q.push(i);
    }
    int now;
    while(!q.empty())
    {
        now = q.front();
        q.pop();
        ++tot;
        for(register int i = 1; i <= n; ++i)
        {
            if(e[now][i])
            {
                for(register int j = 1; j <= n; ++j)
                {
                    if(j != now && e[j][i])
                    {
                        e[j][i] = false;
                        --c[j];
                        if(c[j] == 1) q.push(j);
                    }
                }
                break;
            }
        }
    }
    if(tot < n) return cout << "None", 0;
    for(register int i = 1; i <= n; ++i)
    {
        for(register int j = 1; j <= n; ++j)
        {
            if(e[i][j])
            {
                cout << (char)('A' + i - 1) << " " << j << "\n";
                break;
            }
        }
    }
    return 0;
}
Reference program

 

Guess you like

Origin www.cnblogs.com/kcn999/p/11027861.html