POJ:2585 Window Pains(简单拓扑排序+有难度打map表)

来源:互联网 发布:域名升级访问中sdashao 编辑:程序博客网 时间:2024/05/22 08:09
Window Pains
Time Limit: 1000MS Memory Limit: 65536KTotal Submissions: 1979 Accepted: 997

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

Source

South Central USA 2003

题目大意:电脑是个4*4的屏幕,小明在电脑里开了9个2*2的程序,那些程序的地方如上图所示,然后给你一个图,问通过点击程序,覆盖窗口,是否能够达到所给的图的情况。

解题思路:通过观察,每个点出现的所有情况可以总结出来:


接下来就好办了,你每次输入每个点的情况时,比如你在上图中(2,3)的地方输入了5,说明其余的2,3,6都被5覆盖在下面了,(因为程序们同时存在)这样就能构建map表了,(可以假想成排序,5在2、3、6的前面),然后通过拓扑排序后,就能得到一个点击的顺序了,当然这里只问能否得到,不要顺序,如果得不到,比如2覆盖着3,3覆盖着2,这种情况就显然矛盾了,在拓扑排序中这种矛盾表现为成环,即最终拓扑处理的节点个数不为9(这道题9个程序)。

代码如下:

#include <cstdio>#include <cstring>#include <queue>using namespace std;int qingkuang[4][4][4]={{{1,0,0,0},{1,2,0,0},{2,3,0,0},{3,0,0,0}},{{1,4,0,0},{1,2,4,5},{2,3,5,6},{3,6,0,0}},{{4,7,0,0},{4,5,7,8},{5,6,8,9},{6,9,0,0}},{{7,0,0,0},{7,8,0,0},{8,9,0,0},{9,0,0,0}}};//情况打表 int map[20][20];//储存关系表 int shuru[20][20];//储存你输入的表 int in[20];//入度 int flag;//标记是否矛盾 void tuopu(){queue<int>q;for(int i=1;i<=9;i++){if(in[i]==0){q.push(i);in[i]=-1;}}int sum=0;while(!q.empty()){int tmp=q.front();q.pop();sum++;for(int i=1;i<=9;i++){if(map[tmp][i]==1)//存在关系 {in[i]--;//入度减一 }if(in[i]==0){q.push(i);in[i]=-1;}}}if(sum==9)//共9个程序 {flag=1;}else{flag=0;}}int main(){char s1[30];while(scanf("%s",s1)!=EOF){if(strcmp(s1,"ENDOFINPUT")==0)break;memset(map,0,sizeof(map));memset(in,0,sizeof(in));for(int i=0;i<4;i++){for(int j=0;j<4;j++){scanf("%d",&shuru[i][j]);for(int k=0;k<4;k++){if(qingkuang[i][j][k]==0)//节约时间,上面打的情况表里面有0 break;if(map[shuru[i][j]][qingkuang[i][j][k]]==0&&shuru[i][j]!=qingkuang[i][j][k])//把除了本身的都建立连接,即把它覆盖的程序们建立连接 {map[shuru[i][j]][qingkuang[i][j][k]]=1;in[qingkuang[i][j][k]]++;//被覆盖的入度加1 }}}}char s2[30];scanf("%s",s2);tuopu();if(flag==1){printf("THESE WINDOWS ARE CLEAN\n");}else{printf("THESE WINDOWS ARE BROKEN\n");}}return 0;}


0 0
原创粉丝点击