POJ-2585 Window Pains

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: 
  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 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. 
  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 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的矩阵,里面仅含有1-9这9个元素,每个元素有固定的位置,放的时候会产生顺序和覆盖,给你最后覆盖之后的图,让你判断这个图是不是正确的。

(题目大意根据题意简化= =)

解题思路:

拓扑排序模板题。直接搞就行。只有对矩阵进行处理的时候会有些麻烦。

这题因为数据量小貌似暴力搞也行,推荐拓扑排序。

代码:

#include <queue>
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;

int in[10], m[4][4];
vector<int> vec[10];
vector<int> pre[4][4];

void init(){
    for(int i = 0; i < 4; ++i) for(int j = 0; j < 4; ++j) pre[i][j].clear();
    pre[0][0].push_back(1); pre[0][1].push_back(1); pre[1][0].push_back(1); pre[1][1].push_back(1);
    pre[0][1].push_back(2); pre[0][2].push_back(2); pre[1][1].push_back(2); pre[1][2].push_back(2);
    pre[0][2].push_back(3); pre[0][3].push_back(3); pre[1][2].push_back(3); pre[1][3].push_back(3);
    pre[1][0].push_back(4); pre[1][1].push_back(4); pre[2][0].push_back(4); pre[2][1].push_back(4);
    pre[1][1].push_back(5); pre[1][2].push_back(5); pre[2][1].push_back(5); pre[2][2].push_back(5);
    pre[1][2].push_back(6); pre[1][3].push_back(6); pre[2][2].push_back(6); pre[2][3].push_back(6);
    pre[2][0].push_back(7); pre[2][1].push_back(7); pre[3][0].push_back(7); pre[3][1].push_back(7);
    pre[2][1].push_back(8); pre[2][2].push_back(8); pre[3][1].push_back(8); pre[3][2].push_back(8);
    pre[2][2].push_back(9); pre[2][3].push_back(9); pre[3][2].push_back(9); pre[3][3].push_back(9);
}
bool toposort(){
    priority_queue<int> q;
    while(!q.empty()) q.pop();
    
    for(int i = 1; i < 10; ++i) if(!in[i]) q.push(i);
    
    int cnt = 0;
    while(!q.empty()){
        int p = q.top();
        q.pop(); ++cnt;
        
        int v, len = vec[p].size();
        for(int i = 0; i < len; ++i){
            v = vec[p][i];
            --in[v];
            if(!in[v]) q.push(v);
        }
    }
    
    if(cnt == 9) return true;
    else return false;
}
int main()
{
    // freopen("test.in", "r+", stdin);
    // freopen("test.out", "w+", stdout);
    
    char str[15] = {0}; init();
    while(~scanf(" %s", str)){
        if(strcmp(str, "ENDOFINPUT") == 0) break;
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                scanf("%d", &m[i][j]);
            }
        }
        scanf(" %s", str);
        for(int i = 0; i < 10; ++i) vec[i].clear();
        
        int len, v;
        memset(in, 0, sizeof(in));
        for(int i = 0; i < 4; ++i){
            for(int j = 0; j < 4; ++j){
                len = pre[i][j].size();
                
                for(int k = 0; k < len; ++k){
                    v = pre[i][j][k];
                    if(v == m[i][j]) continue;
                    
                    int flag = 1;
                    for(int tot = 0; tot < vec[v].size() && flag; ++tot){
                        if(vec[v][tot] == m[i][j]) flag = 0;
                    }
                    if(flag) {
                        vec[v].push_back(m[i][j]);
                        ++in[ m[i][j] ];
                    }
                }
            }
        }
        
        if(toposort()){
            printf("THESE WINDOWS ARE CLEAN\n");
        }else{
            printf("THESE WINDOWS ARE BROKEN\n");
        }
    }
    return 0;
}


posted @ 2016-07-24 19:18  _Wilbert  阅读(104)  评论(0编辑  收藏  举报