poj 2585 Window Pains(拓扑排序)(经典)(困难)

Window Pains
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1756   Accepted: 881

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  1 and 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

这题是在《图论算法理论,实现及应用》一书中看到的,按照书上的思路琢磨了蛮久,终于弄懂了。写的时候入度总是弄错,代码都看了四五遍,憋疯了。。。准备把这书扎实啃完。
代码:
#include <iostream>
#include <cstring>
#include <string>
#include <cstdio>
using namespace std;
const int N=5;
int map[10][10];//邻接矩阵
int screen[N][N];//屏幕最后显示的内容
string cover[N][N];//能覆盖(i,j)位置的窗口
string s;
int vis[10];//标记
int id[10];//入度
int t;//记录出现的窗口种类数

void cal()//初始化cover
{
    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);
        cover[i][j+1]+=(char)(k);
        cover[i+1][j]+=(char)(k);
        cover[i+1][j+1]+=(char)(k);
    }
}

void init()
{
    int i,j,k;
    memset(id,0,sizeof(id));
    memset(vis,0,sizeof(vis));
    memset(map,0,sizeof(map));
    t=0;
    for(i=0;i<N-1;i++)
    {
        for(j=0;j<N-1;j++)
        {
            scanf("%d",&k);
            if(!vis[k])
            {
                t++;
                vis[k]=1;
            }
            screen[i][j]=k;
        }
    }
}

void build()//构造有向图
{
    int i,j,k;
    for(i=0;i<N-1;i++)
    {
        for(j=0;j<N-1;j++)
        {
            for(k=0;k<cover[i][j].length();k++)
            {
                if((!map[screen[i][j]][cover[i][j][k]]) && (screen[i][j]!=cover[i][j][k]))
                {
                    map[screen[i][j]][cover[i][j][k]]=1;
                    id[cover[i][j][k]]++;
                }
            }
        }
    }
}

int topo()//拓扑排序,判环
{
    int i,j,k;
    for(i=0;i<t;i++)
    {
        k=1;
        while(!vis[k] || (k<=9&&id[k]>0))
            k++;
        if(k>9)
            return 0;
        vis[k]=0;
        for(j=1;j<10;j++)
        {
            if(vis[j]&&map[k][j])
                id[j]--;
        }
    }
    return 1;
}

int main()
{

    cal();
    while(cin>>s)
    {
        if(s=="ENDOFINPUT")
            break;
        init();
        build();
        if(topo())
            puts("THESE WINDOWS ARE CLEAN");
        else
            puts("THESE WINDOWS ARE BROKEN");
        cin>>s;
    }
    return 0;
}




猜你喜欢

转载自blog.csdn.net/kaisa158/article/details/47106381