Poj2585 window pains topological sorting

Source: Internet
Author: User

Topology Sorting determines that there is no loop (that is, the X Window overwrites the y window, but the y window overwrites the X Window ).

Window pains

Time limit:1000 ms

 

Memory limit:65536 K

Total submissions:1159

 

Accepted:583

Description

Boudreaux likes to multitask, especially when it comes to using his computer. never satisfied with just running one application at a time, he usually runs nine applications, each in its own window. due to limited screen real estate,
He overlaps these windows and brings whatever window he currently needs to work with the foreground. if his screen were a 4x4 grid of squares, each of Boudreaux's windows wocould be represented by the following 2x2 windows:
1 1 . .
1 1 . .
. . . .
. . . .
. 2 2 .
. 2 2 .
. . . .
. . . .
. . 3 3
. . 3 3
. . . .
. . . .
. . . .
4 4 . .
4 4 . .
. . . .
. . . .
. 5 5 .
. 5 5 .
. . . .
. . . .
. . 6 6
. . 6 6
. . . .
. . . .
. . . .
7 7 . .
7 7 . .
. . . .
. . . .
. 8 8 .
. 8 8 .
. . . .
. . . .
. . 9 9
. . 9 9

When Boudreaux brings a window to the foreground, all of its squares come to the top, overlapping any squares it shares with other windows. For example, if window1And thenWindow
2Were brought to the foreground, the resulting representation wocould be:

1 2 2 ?
1 2 2 ?
? ? ? ?
? ? ? ?
If window4Were then brought to the foreground:
1 2 2 ?
4 4 2 ?
4 4 ? ?
? ? ? ?

... And so on...
Unfortunately, Boudreaux's computer is very unreliable and crashes often. he cocould easily tell if a crash occurred by looking at the windows and seeing a graphical representation that shocould not occur if windows were being brought to the foreground correctly.
And this is where you come in...

Input

Input to this problem will consist of a (non-empty) series of up to 100 data sets. each data set will be formatted according to the following description, and there will be no blank lines separating data sets.

A single data set has 3 components:

  1. Start line-a single line:
    Start

  2. Screen Shot-four lines that represent the current graphical representation of the windows on Boudreaux's screen. each position in this 4x4 matrix will represent the current piece of window showing in each square. to make input easier, the list of numbers
    On each line will be delimited by a single space.
  3. End Line-a single line:
    End

After the last data set, there will be a single line:
Endofinput

Note that each piece of visible window will appear only in screen areas where the window cocould appear when brought to the front. For instance, a 1 can only appear in the top left quadrant.

Output

For each data set, there will be exactly one line of output. if there exists a sequence of bringing windows to the foreground that wocould result in the graphical representation of the windows on Boudreaux's screen, the output will
Be a single line with the statement:

These windows are clean

Otherwise, the output will be a single line with the statement:
These windows are broken

Sample Input

START1 2 3 34 5 6 67 8 9 97 8 9 9ENDSTART1 1 3 34 1 3 37 7 9 97 7 9 9ENDENDOFINPUT

Sample output

THESE WINDOWS ARE CLEANTHESE WINDOWS ARE BROKEN
# Include <iostream> # include <queue> # include <vector> # include <string> # include <cstring> using namespace STD; int maps [5] [5]; vector <int> G [10]; int counts [10]; string s; bool topsort () {int n = 9; int sum = N; // n total number of nodes queue <int> q; bool flag = false; For (INT I = 1; I <= N; I ++) // The inbound value is 0. {If (counts [I] = 0) Q. push (I) ;}while (! Q. empty () {sum --; Int J = Q. front (); q. pop (); For (int K = 0; k <G [J]. size (); k ++) {If (-- counts [G [J] [k] = 0) Q. push (G [J] [k]) ;}} if (sum> 0) return false; // indicates that the loop return true exists; // OK} void Init () {for (INT I = 0; I <= 9; I ++) {counts [I] = 0; G [I]. clear () ;}for (INT I = 1; I <= 4; I ++) {for (Int J = 1; j <= 4; j ++) {CIN> maps [I] [J] ;}} for (INT I = 1; I <= 9; I ++) // create a graph {int X, Y; X = (I-1)/3 + 1; y = (I % 3 = 0? 3: I % 3); If (maps [x] [Y]! = I) {G [maps [x] [Y]. push_back (I); counts [I] ++;} If (maps [x] [Y + 1]! = I) {G [maps [x] [Y + 1]. push_back (I); counts [I] ++;} If (maps [x + 1] [Y]! = I) {G [maps [x + 1] [Y]. push_back (I); counts [I] ++;} If (maps [x + 1] [Y + 1]! = I) {G [maps [x + 1] [Y + 1]. push_back (I); counts [I] ++;} CIN> S;} void solve () {If (topsort ()) cout <"these windows are clean" <Endl; elsecout <"these windows are broken" <Endl;} int main () {While (CIN> S & S! = "Endofinput") {Init (); solve ();} 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.