poj 2585& zoj 2193
2013-08-18 20:59
381 查看
Window Pains
Time Limit: 2 Seconds Memory Limit: 65536 KB
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:
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 1 and then window 2were
brought to the foreground, the resulting representation would be:
. . . 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 linesseparating data sets.
A single data set has 3 components:
Start line - A single line:
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:
After the last data set, there will be a single line:
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:
Otherwise, the output will be a single line with the statement:
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
一开始没有看出来是拓扑排序,后来其实一想就是按照给出的屏幕构建有向的网络,构建的原则就是通过遮盖的关系,遮挡的放前,被遮的放后,然后通过拓扑判断图内是否有环,有的话,就说明死机了。
下面是代码;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int n=4;
int screen[4][4];
string cover[4][4];
bool exist[10];
int id[10];
bool g[10][10];
int t;
string s;
void calc(){
int k,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;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');//第k个窗口的左上角位置
cover[i][j+1]+=char(k+'0');//第k个窗口的右上角位置
cover[i+1][j]+=char(k+'0');//第k个窗口的左下角位置
cover[i+1][j+1]+=char(k+'0');//第k个窗口的右下角位置
}
}
void init(){//读入屏幕数据
int i,j,k;
memset(exist,0,sizeof(exist));
memset(id,0,sizeof(id));
memset(g,0,sizeof(g));
t=0;//屏幕中出现的窗口种类
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>k;
screen[i][j]=k;
if(!exist[k])
t++;
exist[k]=true;
}
}
void build(){//构建有向图
int i,j,p;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
for(p=0;p<cover[i][j].length();p++){
if((!g[screen[i][j]][cover[i][j][p]-'0'])&&(screen[i][j]!=cover[i][j][p]-'0'))
{
g[screen[i][j]][cover[i][j][p]-'0']=true;
id[cover[i][j][p]-'0']++;
}
}
}
}
bool check(){//判断图内是否有环
int i,j,k;
for(k=0;k<t;k++)
{
i=1;
while(!exist[i]||(i<=9&&id[i]>0))
i++;
if(i>9)
return false;
exist[i]=false;
for(j=1;j<=9;j++)
if(exist[j]&&g[i][j])
id[j]--;
}
return true;
}
int main(){
calc();
string str = "ENDOFINPUT";
while(cin>>s){
if(s== str) break;
init();
build();
if(check())
cout<<"THESE WINDOWS ARE CLEAN"<<endl;
else
cout<<"THESE WINDOWS ARE BROKEN"<<endl;
cin>>s;
}
return 0;
}
Time Limit: 2 Seconds Memory Limit: 65536 KB
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:
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
brought to the foreground, the resulting representation would be:
| If window 4 were then brought to the foreground: |
|
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 linesseparating 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
一开始没有看出来是拓扑排序,后来其实一想就是按照给出的屏幕构建有向的网络,构建的原则就是通过遮盖的关系,遮挡的放前,被遮的放后,然后通过拓扑判断图内是否有环,有的话,就说明死机了。
下面是代码;
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdlib>
using namespace std;
const int n=4;
int screen[4][4];
string cover[4][4];
bool exist[10];
int id[10];
bool g[10][10];
int t;
string s;
void calc(){
int k,i,j;
for(i=0;i<n;i++)
for(j=0;j<n;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');//第k个窗口的左上角位置
cover[i][j+1]+=char(k+'0');//第k个窗口的右上角位置
cover[i+1][j]+=char(k+'0');//第k个窗口的左下角位置
cover[i+1][j+1]+=char(k+'0');//第k个窗口的右下角位置
}
}
void init(){//读入屏幕数据
int i,j,k;
memset(exist,0,sizeof(exist));
memset(id,0,sizeof(id));
memset(g,0,sizeof(g));
t=0;//屏幕中出现的窗口种类
for(i=0;i<n;i++)
for(j=0;j<n;j++){
cin>>k;
screen[i][j]=k;
if(!exist[k])
t++;
exist[k]=true;
}
}
void build(){//构建有向图
int i,j,p;
for(i=0;i<n;i++)
for(j=0;j<n;j++){
for(p=0;p<cover[i][j].length();p++){
if((!g[screen[i][j]][cover[i][j][p]-'0'])&&(screen[i][j]!=cover[i][j][p]-'0'))
{
g[screen[i][j]][cover[i][j][p]-'0']=true;
id[cover[i][j][p]-'0']++;
}
}
}
}
bool check(){//判断图内是否有环
int i,j,k;
for(k=0;k<t;k++)
{
i=1;
while(!exist[i]||(i<=9&&id[i]>0))
i++;
if(i>9)
return false;
exist[i]=false;
for(j=1;j<=9;j++)
if(exist[j]&&g[i][j])
id[j]--;
}
return true;
}
int main(){
calc();
string str = "ENDOFINPUT";
while(cin>>s){
if(s== str) break;
init();
build();
if(check())
cout<<"THESE WINDOWS ARE CLEAN"<<endl;
else
cout<<"THESE WINDOWS ARE BROKEN"<<endl;
cin>>s;
}
return 0;
}
相关文章推荐
- zoj 2193 && poj 2585 Window Pains
- POJ 2777 && ZOJ 1610 &&HDU 1698 --线段树--区间更新
- poj 1511&zoj 2008 最短路
- ZOJ 1978 && HDU 1216 && POJ 2552 Assistance Required (筛选)
- poj 1032 && zoj 1265 Area
- Poj 1967 &&Zoj 2183 Alibaba
- zoj 1914 || poj 2349 Arctic Network【最小生成树 kruskal && prim】
- POJ 1862 & ZOJ 1543 Stripies(贪心 | 优先队列)
- ZOJ 1041(POJ 1106) Transmitters(…
- poj 1689 && zoj 1422 3002 Rubbery (Geometry + BFS)
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
- poj 1543 & HDU 1334 & ZOJ 1331 Perfect Cubes(数学 暴力大法好)
- ZOJ 1683 Fishnet (POJ 1408)(计算线段交点&叉积求面积)
- POJ 3654 & ZOJ 2936 & HDU 2723 Electronic Document Security(模拟)
- POJ 1861 && ZOJ 1542--Network 【最小生成树 && kruscal && 水题】
- poj 1637 & zoj 1992 混合图的欧拉回路
- ZOJ 1760 &&POJ1552 Doubles (模拟)
- ZOJ 1504(POJ 1244) Slots of Fun…
- POJ 2029 && ZOJ 1716 Get Many Persimmon Trees
- POJ 2546 & ZOJ 1597 Circular Area(求两圆相交的面积 模板)