POJ 2585 拓扑排序
来源:互联网 发布:android完整浏览器源码 编辑:程序博客网 时间:2024/05/21 11:09
Description
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
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
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;}
- POJ 2585 拓扑排序
- POJ 2585 拓扑排序
- POJ 2585 Window Pains 拓扑排序
- POJ 2585 Window Pains (拓扑排序)
- POJ:2585 Window Pains(拓扑排序)
- [POJ 2585] Window Pains (拓扑排序)
- POJ 2585 Window Pains(拓扑排序判定)
- poj 2585 Window Pains 拓扑排序
- POJ--2585--Window Pains【拓扑排序】
- POJ 2585 Window Pains 拓扑排序
- POJ 2585 Window Pains(拓扑排序)
- poj 2585 Window Pains(拓扑排序)
- 拓扑排序,poj 1094
- 拓扑排序,poj 1094
- POJ 1094 拓扑排序
- poj-1094拓扑排序
- poj 1094 拓扑排序
- POJ 3687 拓扑排序
- 字体加阴影效果
- window下如何配置apache Http服务器使其能运行python cgi脚本
- centOS无线网卡驱动
- 三层 实例理解
- 基于Hadoop1.0.4的分布式云网盘(转载)
- POJ 2585 拓扑排序
- SupremeWord进度23.7%
- java类初始化顺序
- Ubuntu环境下CodeBlock报错“undefined reference”的解决方法
- 第10章课程设计1
- eclipse出现乱码
- Android 布局优化
- 基于Github参与eoe的开源项目指南
- 高性能网络编程7--tcp连接的内存使用