POJ 2585 拓扑排序

来源:互联网 发布:android完整浏览器源码 编辑:程序博客网 时间:2024/05/21 11:09

Window Pains
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1462 Accepted: 736

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..99When 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: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

START1 2 3 34 5 6 67 8 9 97 8 9 9ENDSTART1 1 3 34 1 3 37 7 9 97 7 9 9ENDENDOFINPUT

Sample Output

THESE WINDOWS ARE CLEANTHESE WINDOWS ARE BROKEN
题意:Boudreaux 喜欢多任务的系统,特别是当他用计算机时。他从不满足于每次只运行一个程序,
通常他总是同时运行9 个程序,每个程序有一个窗口。由于显示器屏幕大小有限,他把窗口重叠,
并且当他想用某个窗口时,他就把它调到最前面。如果他的显示器是一个4×4 的网格,则Boudreaux
的每一个程序窗口就应该像图2.32 那样用2×2 大小的窗口表示。

当Boudreaux 把一个窗口调到最前面时,它的所有方格都位于最前面,覆盖它与其它窗口共
用的方格。例如,如果先是窗口1 位于最前面,然后是窗口2,那么结果应为图2.33(a)所示。如
果接下来窗口4 位于最前面,则结果应为图2.33(b)所示,等等。

不幸的是,Boudreaux 的电脑很不稳定,经常崩溃。他通过观察这些窗口,判断出如果每个窗
口都被正确地调到最前面时窗口表示不应该是现在这种图形,就能判断出电脑是死机了。
输入描述:
输入文件包含最多100 组数据。每组数据将按如下格式给出,各组数据间无空行。
每组数据包含3 部分:
(1) 起始行 - 为字符串"START";
(2) 显示器屏幕快照 - 用四行表示当前Boudreaux 的显示器状态。这4 行为4×4 的矩阵,每
一个元素代表显示器对应方格中所显示的一小块窗口。为使输入简单点,数字间仅用一个空格分
开。
(3) 结束行 - 为字符串"END"。
最后一组数据后,会有一行,为字符串"ENDOFINPUT"。
注意每个小块只能出现在它可能出现的地方。例如1 只能出现在左上方4 个方格里。
第2 章 图的遍历与活动网络问题
- 73 -
输出描述:
对每个数据只输出一行:如果能按一定顺序依次将每个窗口调到最前面时能达到数据描述的
那样(即没死机),输出"THESE Windows ARE CLEAN",否则输出"THESE Windows ARE BROKEN"。

思路:

转化成有向无环图确实有点难度,刚开始学,自己想是不太明白的,看了书上的解释确实发现图论太神了……

比较费神的拓扑排序,是个好题,,,

#include <iostream>#include <cstdio>#include <fstream>#include <algorithm>#include <cmath>#include <deque>#include <vector>#include <stack>#include <list>#include <queue>#include <string>#include <cstring>#include <map>#define PI acos(-1.0)#define mem(a,b) memset(a,b,sizeof(a))#define sca(a) scanf("%d",&a)#define M 5010#define INF 10000000using namespace std;typedef long long ll;int id[10],screen[4][4],Map[10][10]; //入度,屏幕,标记数组string s,cover[4][4]; //cover为原始定义4*4方格中每格可以有哪些数字vector<vector<int> >v; //由u->v的数组stack<int>q;void calc(){    int k,i,j;    for(i=0;i<4;i++)        for(j=0;j<4;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');        cover[i][j+1]+=char(k+'0');        cover[i+1][j]+=char(k+'0');        cover[i+1][j+1]+=char(k+'0');    }}void init(){    int i,j;    mem(id,0); mem(Map,0);    v.clear(); v.resize(10);    for(i=0;i<4;i++)        for(j=0;j<4;j++)            cin>>screen[i][j];}void build(){    int i,j,k;    for(i=0;i<4;i++)        for(j=0;j<4;j++)            for(k=0;k<cover[i][j].length();k++)                if(!Map[screen[i][j]][cover[i][j][k]-'0']&&screen[i][j]!=cover[i][j][k]-'0')                {                    Map[screen[i][j]][cover[i][j][k]-'0']=1;                    id[cover[i][j][k]-'0']++;                    v[screen[i][j]].push_back(cover[i][j][k]-'0');                }}bool topsort(){    int i,j,num=0;    for(i=1;i<=9;i++)        if(!id[i]) q.push(i);    while(!q.empty())    {        j=q.top(); q.pop();        num++;        for(i=0;i<v[j].size();i++)            if(--id[v[j][i]]==0) q.push(v[j][i]);    }    if(num==9) return true;  //有环    return false;  //存在拓扑排序}int main(){    calc();    while(cin>>s&&s!="ENDOFINPUT")    {        init();        build();        if(topsort()) cout<<"THESE WINDOWS ARE CLEAN"<<endl;        else cout<<"THESE WINDOWS ARE BROKEN"<<endl;        cin>>s;    }    return 0;}

0 0
原创粉丝点击