POJ 2585 Window Pains (窗口绘制)(拓扑排序)

来源:互联网 发布:lol网吧代理软件 编辑:程序博客网 时间:2024/05/01 04:10
Window Pains
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1898 Accepted: 951
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 to the foreground. If his screen were a 4 x 4 grid of squares, each of Boudreaux's windows would be represented by the following 2 x 2 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 window 1and then window 2 were brought to the foreground, the resulting representation would be:
1 2 2 ?
1 2 2 ?
? ? ? ?
? ? ? ?
If window 4 were 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 could easily tell if a crash occurred by looking at the windows and seeing a graphical representation that should 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:
Start line - A single line:
START

Screen Shot - Four lines that represent the current graphical representation of the windows on Boudreaux's screen. Each position in this 4 x 4 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.
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 could 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 would 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

START
1 2 3 3
4 5 6 6
7 8 9 9
7 8 9 9
END
START
1 1 3 3
4 1 3 3
7 7 9 9
7 7 9 9
END
ENDOFINPUT

Sample Output

THESE WINDOWS ARE CLEAN

THESE WINDOWS ARE BROKEN


题解:

有个4*4的矩阵,电脑桌面,桌面上有9个程序的窗口,每个程序的窗口都有固定的位置(且只能在那个位置,当然,窗口与窗口之间有重叠)。具体的位置观察上面的图片。

输入一组4*4的矩阵,判断能否将9个程序的窗口相叠,并叠出来与输入数据一样的界面。

能输出:THESE WINDOWS ARE CLEAN。

不能输出:THESE WINDOWS ARE BROKEN

求解过程:

(1)通过给出的屏幕构建有向网络

(2)通过拓扑排序算法判断该网络是否有环,如果存在环,则该屏幕为死机后屏幕,否则为正常屏幕。


代码:


///窗口的绘制#include<iostream>#include<string>using namespace std;int a[4][4];int indegree[9]= {0}; ///入度数组int stack[9];///栈int top=-1;///栈顶指针bool bian[9][9]= {0}; ///边void Iopo_Sort()///拓扑排序{    int i,j,k;    for(i=0; i<9; i++)        if(indegree[i]==0)            stack[++top]=i;    i=0;    int sum=0;    while(top>=0)    {        j=stack[top--];        for(k=0; k<9; k++)            if(bian[j][k])            {                indegree[k]--;                if(indegree[k]==0)                    stack[++top]=k;            }        sum++;    }        if(sum<9)cout<<"THESE WINDOWS ARE BROKEN"<<endl;    else cout<<"THESE WINDOWS ARE CLEAN"<<endl;}int main(){    int i,j,k;    string c,d;    d="ENDOFINPUT";    cin>>c;///输入START    while(c!=d)///判断c是否为ENDOFINPUT    {        ///输入数据        for(i=0; i<4; i++)            for(j=0; j<4; j++)                cin>>a[i][j];        cin>>c;///输入END        ///初始化入读数组和边        for(i=0; i<9; i++)        {            indegree[i]=0;            for(j=0; j<9; j++)                bian[i][j]=0;        }        top=-1;        int t1,t2,t3,t4;        for(i=0; i<9; i++)        {            ///确定i的位置            j=i/3;///行            k=i%3;///列            t1=a[j][k];            t2=a[j][k+1];            t3=a[j+1][k];            t4=a[j+1][k+1];            ///构造i对应的边            if((t1-1)!=i)///不能等于自己            {                indegree[t1-1]++;                bian[i][t1-1]=1;            }            if((t2-1)!=i&&t2!=t1)///不能等于自己且不能等于t1,不然会重复构造边,相应节点的入读会增加            {                indegree[t2-1]++;                bian[i][t2-1]=1;            }            if((t3-1)!=i&&t3!=t1)///不能等于自己且不能等于t1,不然会重复构造边,相应节点的入读会增加            {                indegree[t3-1]++;                bian[i][t3-1]=1;            }            if((t4-1)!=i&&t4!=t3&&t4!=t2)///不能等于自己且不能等于t1,不然会重复构造边,相应节点的入读会增加            {                indegree[t4-1]++;                bian[i][t4-1]=1;            }        }        Iopo_Sort();        cin>>c;    }    return 0;}


0 0