欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

poj2585 Window Pains

程序员文章站 2022-05-13 11:21:39
...

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

分析:
窗口肯定是一个叠一个的
这就存在一个上下关系
就好比关闭窗口,只有关闭覆盖某一窗口所有窗口
才能关闭这个窗口

分析到这一步,就慢慢地看见一些数据结构的雏形了
那到底是什么算法,可以层次分明,只有把上层的所有元素都遍历了,才能遍历下层呢
top排序啊

在整个屏幕中,有几个点是可能发生重叠现象的

0120
3456
789A
0BC0

1:1 2
2:2 3
3:1 4
4:1 2 4 5
5:2 3 5 6
6:3 6
7:4 7
8:4 5 7 8
9:5 6 8 9
A: 6 9
B: 7 8
C: 8 9

这些位置上是什么数字,就说明其余的都被其覆盖了
我们以此建出一个图,只要这个图可以进行top,
就说明该状态合法

附样例一的图
poj2585 Window Pains

tip

不要忘了初始化(in,st,tot,top,mp)

这里写代码片
#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

const int INF=0x33333333;
struct node{
    int x,y,nxt;
};
node way[410];
int tot=0,st[30],in[30],sta[30],top=0,n;
int mp[10][10];
int d[20][5]={{0,0,0,0,0},{0,0,0,0,0},{2,1,2,0,0},{2,2,3,0,0},  //每个位置上出现的窗口 
              {0,0,0,0,0},{2,1,4,0,0},{4,1,2,4,5},{4,2,3,5,6},
              {2,3,6,0,0},{2,4,7,0,0},{4,4,5,7,8},{4,5,6,8,9},
              {2,6,9,0,0},{0,0,0,0,0},{2,7,8,0,0},{2,8,9,0,0},
              {0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0},{0,0,0,0,0}};

void add(int u,int w)
{
    tot++;
    way[tot].x=u;way[tot].y=w;way[tot].nxt=st[u];st[u]=tot;
}              

int get(int x,int y){return (x-1)*4+y;} 

int _top()
{
    top=0;
    int i,j,tt=0;
    for (i=1;i<=n;i++)
        if (in[i]==0)
            sta[++top]=i,tt++,in[i]=INF;
    while (top)
    {
        int r=sta[top--],k=0;
        for (int i=st[r];i;i=way[i].nxt)
        {
            in[way[i].y]--;
            if (in[way[i].y]==0)
            {
                sta[++top]=way[i].y;
                k=1;
                tt++;
                in[way[i].y]=INF;
            }
        }
    }
    if (tt!=n) return 0;
    else return 1;
}

int main()
{
    char opt[20];
    scanf("%s",&opt);
    while (opt[4]!='F')
    {
        n=9;
        memset(mp,0,sizeof(mp));
        memset(st,0,sizeof(st));
        memset(in,0,sizeof(in));
        tot=0;
        for (int i=1;i<=4;i++)
            for (int j=1;j<=4;j++)
            {
                int u;
                scanf("%d",&u);
                int bh=get(i,j);
                for (int k=1;k<=d[bh][0];k++)
                    if (d[bh][k]!=u&&mp[u][d[bh][k]]==0)
                    {
                        add(u,d[bh][k]);
                        in[d[bh][k]]++;   //上层向下层连边,入度++ 
                        mp[u][d[bh][k]]=1;
                    }
            }
        if (_top()) printf("THESE WINDOWS ARE CLEAN\n");
        else printf("THESE WINDOWS ARE BROKEN\n");
        scanf("%s",&opt);
        scanf("%s",&opt);
    }
    return 0;
}
相关标签: top