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:
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
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 1 and then window 2were brought to the foreground, the resulting representation would be:
| If window 4 were then brought to the foreground: |
|
. . . 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 linesseparating 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 CLEANOtherwise, 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
一开始没有看出来是拓扑排序,后来其实一想就是按照给出的屏幕构建有向的网络,构建的原则就是通过遮盖的关系,遮挡的放前,被遮的放后,然后通过拓扑判断图内是否有环,有的话,就说明死机了。
下面是代码;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int n=4;
int screen[4][4];
string cover[4][4];
bool exist[10];
int id[10];
bool g[10][10];
int t;
string s;
void calc(){
int k,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cover[i][j].erase();//固定长度的数组初始化
for(k=1;k<=9;k++){
i=(k-1)/3;
j=(k-1)%3;
cover[i][j]+=char(k+'0');//第k个窗口的左上角位置
cover[i][j+1]+=char(k+'0');//第k个窗口的右上角位置
cover[i+1][j]+=char(k+'0');//第k个窗口的左下角位置
cover[i+1][j+1]+=char(k+'0');//第k个窗口的右下角位置
}
}
void init(){//读入屏幕数据
int i,j,k;
memset(exist,0,sizeof(exist));
memset(id,0,sizeof(id));
memset(g,0,sizeof(g));
t=0;//屏幕中出现的窗口种类
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>k;
screen[i][j]=k;
if(!exist[k])
t++;
exist[k]=true;
}
}
void build(){//构建有向图
int i,j,p;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
for(p=0;p<cover[i][j].length();p++){
if((!g[screen[i][j]][cover[i][j][p]-'0'])&&(screen[i][j]!=cover[i][j][p]-'0'))
{
g[screen[i][j]][cover[i][j][p]-'0']=true;
id[cover[i][j][p]-'0']++;
}
}
}
}
bool check(){//判断图内是否有环
int i,j,k;
for(k=0;k<t;k++)
{
i=1;
while(!exist[i]||(i<=9&&id[i]>0))
i++;
if(i>9)
return false;
exist[i]=false;
for(j=1;j<=9;j++)
if(exist[j]&&g[i][j])
id[j]--;
}
return true;
}
int main(){
calc();
string str = "ENDOFINPUT";
while(cin>>s){
if(s== str) break;
init();
build();
if(check())
cout<<"THESE WINDOWS ARE CLEAN"<<endl;
else
cout<<"THESE WINDOWS ARE BROKEN"<<endl;
cin>>s;
}
return 0;
}