POJ-2585-Window Pains

<center> Window Pains </center> <center> Time Limit: 1000MS Memory Limit: 65536K </center> <center> Total Submissions: 2915 Accepted: 1461 </center>

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:

<center> </center>
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 

不得不说Poj的题就是难,想了好久但是可算有点思路了,结果交了一发,直接Wa,这谁受得了呀,后来找了好几篇博客才看明白,因为每个方格都会有一组固定的几个数,我们事先将每个方格里面的数打标打出来,然后在输入图形的时候进行处理,输入的谁,谁就就大于其他值,例如:(3,3)这个位置,可能出现的数字是:5,6,8,9;如果在输入的时候,输入的是8,那就在构建拓排图的时候让8>6,8>6,8>9;遇到自身的话就跳过,这样构好图后就按照平常的拓排进行跑就行了,只有又环的出现那么久不满足情况,另外要注意去重,应为方格的情况可能冲突所以要注意去重;

#include<iostream>
#include<queue>
#include<vector>
#include<string.h>
#include<string>
#include<set>
using namespace std;

typedef pair<int,int> pa;
vector<int>v[100];
int mp[5][5];
string s;
int in[20];
set<pa>se;
int mmp[4][4][6]={                 //前两个4表示的是位置,第几行第几列,最后一个6表示里面可能有的数字, 
{{1,1},{2,1,2},{2,2,3},{1,3}}	//注意我的每一个开始的数字是表示这个位置可能有几个数字
,{{2,1,4},{4,1,2,4,5},{4,2,3,5,6},{2,3,6}}//这样的话方便以后的构图
,{{2,4,7},{4,4,5,7,8},{4,5,6,8,9},{2,6,9}},
{{1,7},{2,7,8},{2,8,9},{1,9}}
};
void gt(int x,int y)//大概的构造图形,把每个点的大小关系都放进set,进行去重;
{
	for(int i=1;i<=mmp[x][y][0];i++)
	{
		int xx=mmp[x][y][i];
		if(xx!=mp[x][y])//如果相等就不放进去
		{
			se.insert(make_pair(xx,mp[x][y]));
		}
	}
}

int main()
{
	while(cin>>s)
	{
		if(s=="ENDOFINPUT") break;	
	// 数据初始化,很重要!!!
		se.clear();
		for(int i=0;i<10;i++) v[i].clear();
		memset(in, 0,sizeof in);
		
		for(int i=0;i<4;i++)
		{
			for(int j=0;j<4;j++)
			{
				cin>>mp[i][j];
				gt(i,j);//进行大小关系的确定
			}
		}
		cin>>s;
		
		pa p;
		set<pa>::iterator it;//从set里面取数,然后开始正式的构建拓排有向图
		for(it=se.begin();it!=se.end();it++)//
		{
			p=*it;
			int x=p.first ;
			int y=p.second ;
			v[x].push_back(y);
			in[y]++; //入度加一
		}
	//查找入度为零的节点,从入度为零的节点开始删除
		queue<int>q;
		for(int i=1;i<10;i++)
		{
			if(!in[i]) q.push(i);  
		}
		int k=9;
	//拓排删除节点
		while(!q.empty() )
		{
			int x=q.front() ;q.pop() ;
			k--;
			for(int i=0;i<v[x].size() ;i++)
			{
				in[v[x][i]]--;//都一个节点度数减少一
				if(!in[v[x][i]]) q.push(v[x][i]); 
			}
		}
//如果k最后不等0,说明还有节点没被删除,也就是说形成了环
		if(k) cout<<"THESE WINDOWS ARE BROKEN"<<endl;
	    else cout<<"THESE WINDOWS ARE CLEAN"<<endl;
	}
	return 0;
}