poj2585 Window Pains

来源:互联网 发布:linux内核情景分析pdf 编辑:程序博客网 时间:2024/05/22 12:07

这是题目:

3:Window Pains
查看 提交 统计 提问
总时间限制: 1000ms 内存限制: 65536kB
描述
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 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.
输出
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

样例输入
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

样例输出
THESE WINDOWS ARE CLEAN
THESE WINDOWS ARE BROKEN

来源
South Central USA 2003

===================================================================================================================

拓扑排序。在某个程序p的位置上若出现其他程序(如a, b, c, d)的标号,那么其他程序一定在p之后运行(若不然...),由此可得有向边(p-->a), (p-->b), (p-->c), (p-->d),扫描输入矩阵,即可构图。若正常,则这个图是DAG,所有顶点可以排成一个拓扑序列;若不正常,则图中有环(p不可能既在a之前,又在a之后运行),因此不是所有顶点都可以排成一个拓扑序列。


代码清单:

#include <iostream>#include <string>#include <stack>//用于实现拓扑排序:存储入度为0的顶点using namespace std;#define WINSZ 4#define BLOCKSZ 2#define MAXVERTEX 9#define INFINITE 999999999#define CONNECTED 1int adjMatrix[MAXVERTEX][MAXVERTEX];//邻接矩阵int inDegree[MAXVERTEX];//入度表void init(){for (int i=0; i<MAXVERTEX; ++i){inDegree[i]=0;for (int j=0; j<MAXVERTEX; ++j){adjMatrix[i][j]=INFINITE;}}}void buildGraph(int m[][WINSZ]){int x, y;//每一程序块左上角点在矩阵中的坐标int dx, dy;//用于遍历一个程序块中的数字int current;for (int i=0; i<MAXVERTEX; ++i){x=i%(WINSZ-1);y=i/(WINSZ-1);for (dy=0; dy<BLOCKSZ; ++dy){for (dx=0; dx<BLOCKSZ; ++dx){current=m[y+dy][x+dx];if(current!=i && adjMatrix[i][current]==INFINITE)//在程序块i中出现的其他程序块current只能在i之后,即,有边(i-->current)。另外注意不要重复计算入度。{adjMatrix[i][current]=CONNECTED;++inDegree[current];}}}}}bool toposortable()//所有顶点是否可形成一个拓扑序列。如果有环,当然不能形成拓扑序列。{stack<int> zeroInD;for (int i=0; i<MAXVERTEX; ++i){if(inDegree[i]==0){zeroInD.push(i);}}while (!zeroInD.empty()){int current=zeroInD.top();zeroInD.pop();for (int i=0; i<MAXVERTEX; ++i){if (adjMatrix[current][i] == CONNECTED)//发现一条以current为起点的有向边{--inDegree[i];//删掉这条边,终点的入度-1if(inDegree[i] == 0){zeroInD.push(i);}}}}//检查拓扑序列是否包含了所有的顶点,因为若有环,环上的顶点绝不可能在拓扑序列中。//检查最终是否所有顶点的入度都减为0。若有顶点入度不为0,说明不在拓扑序列中。for (int i=0; i<MAXVERTEX; ++i){if (inDegree[i] != 0){return false;}}return true;}int main(){freopen("D:\\in.txt", "r", stdin);freopen("D:\\out.txt", "w", stdout);int inputMat[WINSZ][WINSZ];string tempStr;int tempInt;while (1){cin>>tempStr;if(tempStr == "ENDOFINPUT")break;for (int i=0; i<WINSZ; ++i){for (int j=0; j<WINSZ; ++j){cin>>tempInt;inputMat[i][j]=tempInt-1;}}init();buildGraph(inputMat);if (toposortable())cout<<"THESE WINDOWS ARE CLEAN"<<endl;else cout<<"THESE WINDOWS ARE BROKEN"<<endl;cin>>tempStr;}return 0;}


0 0
原创粉丝点击