POJ 2585 Window Pains【拓扑】

Window Pains

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 1778 Accepted: 894

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:
11..
11..
....
....
.22.
.22.
....
....
..33
..33
....
....
....
44..
44..
....
....
.55.
.55.
....
....
..66
..66
....
....
....
77..
77..
....
....
.88.
.88.
....
....
..99
..99
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:
122?
122?
????
????
If window 4 were then brought to the foreground:
122?
442?
44??
????
. . . 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的屏幕,按题上的图所示,每一个不同的2*2的方块表示一定数字,然后给你一个4*4的矩阵,问该矩阵能否通过一定顺序放1~9小方块来实现,例如第一个数据,可以这样放1->2->3->4->5->6->7->8->9这样放,最后就是所给数据了,然后建图时,例如第一组数据中的2表示第二块可以覆盖第一块来实现则让2指向1最后判断图是否成环,若成环则不可以,因为不存在2覆盖1而1又覆盖2的情况

因为数组忘记清零WA了我一上午。。。。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
int cnt,in[10],head[10],map[5][5];
int jd[4][4][4]={{{1},{1,2},{2,3},{3}},{{1,4},{1,2,4,5},{2,3,5,6},{3,6}},{{4,7},{4,5,7,8},{5,6,8,9},{6,9}},{{7},{7,8},{8,9},{9}}};
char s[15],e[6];//上面一行的三维数组中最里面那一层的意思是jd[i][j]这个位置同属于哪几块窗口,例如jd[2][2]={4,5,7,8}意思是该位置同时属于4,5,7,8窗口
struct node
{
    int a,b,next;
};
node edge[100];
void add(int a,int b)
{
    edge[cnt].a=a;
    edge[cnt].b=b;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
void top_sort()
{
    queue<int>q;
    int num=0;
    for(int i=1;i<=9;++i)
    {
        if(in[i]==0)
            q.push(i);
    }
    int u,v;
    while(!q.empty())
    {
        u=q.front();
        num++;
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            v=edge[i].b;
            in[v]--;
            if(in[v]==0)
                q.push(v);
        }
    }
    if(num==9)
        printf("THESE WINDOWS ARE CLEAN\n");
    else
        printf("THESE WINDOWS ARE BROKEN\n");
}
int main()
{
    while(~scanf("%s",s))
    {
        cnt=0;
        memset(in,0,sizeof(in));
        memset(head,-1,sizeof(head));
        if(strcmp(s,"ENDOFINPUT")==0)
            break;
        for(int i=0;i<4;++i)
        {
            for(int j=0;j<4;++j)
            {
                scanf("%d",&map[i][j]);
                /*for(int k=0;k<4;++k)//这个是看的康神的博客,用三维数组存,几行代码取代我几十行。。。。
                {
                    if(jd[i][j][k]&&jd[i][j][k]!=map[i][j])
                    {
                        add(map[i][j],jd[i][j][k]);
                        in[jd[i][j][k]]++;
                    }
                }*/
                if(i>=0&&i<=1)
                {
                    if(j>=0&&j<=1&&map[i][j]!=1)
                    {
                        in[1]++;
                        add(map[i][j],1);
                    }
                    if(j>=1&&j<=2&&map[i][j]!=2)
                    {
                        in[2]++;
                        add(map[i][j],2);
                    }
                    if(j>=2&&j<=3&&map[i][j]!=3)
                    {
                        in[3]++;
                        add(map[i][j],3);
                    }
                }
                if(i>=1&&i<=2)
                {
                    if(j>=0&&j<=1&&map[i][j]!=4)
                    {
                        in[4]++;
                        add(map[i][j],4);
                    }
                    if(j>=1&&j<=2&&map[i][j]!=5)
                    {
                        in[5]++;
                        add(map[i][j],5);
                    }
                    if(j>=2&&j<=3&&map[i][j]!=6)
                    {
                        in[6]++;
                        add(map[i][j],6);
                    }
                }
                if(i>=2&&i<=3)
                {
                    if(j>=0&&j<=1&&map[i][j]!=7)
                    {
                        in[7]++;
                        add(map[i][j],7);
                    }
                    if(j>=1&&j<=2&&map[i][j]!=8)
                    {
                        in[8]++;
                        add(map[i][j],8);
                    }
                    if(j>=2&&j<=3&&map[i][j]!=9)
                    {
                        in[9]++;
                        add(map[i][j],9);
                    }
                }
           }
        }
        scanf("%s",e);
        top_sort();
    }
    return 0;
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值