poj2585 Window Pains(拓扑排序判定)
2016-02-02 19:30
405 查看
题意:给你一个4*4的棋盘窗口,现在电脑上有9个应用,每个应用占用固定的2*2正方形网格位置.你通过不同的顺序操作9个应用可以使得4*4的窗口当前显示的内容(数字代表)不同,现在给你一个4*4棋盘窗口的内容,问你这个内容是否合法.
思路:一个判定拓扑排序是否存在的问题.把9个小窗口应用看成9个点,然后对于4*4的棋盘来说: 看(1,2)这个方格当前放的是2数字,原本(1,2)方格我们能放的数有1和2两个数,现在放了2,说明从1应用->2应用有一条有向边.(也即是说应用1应先放,应用2后放,所以应用2能遮盖应用1).
现在来看(2,2)这个方格,这个方格是5,但是这个方格原先能放的应用有:1,2,4,5 四个应用.现在出现的应用是5,说明5是最后才放的应用.所以我们可知从应用1->5应用有一条有向边,从2->5,4->5都有一条有向边.
Trick:程序中有可能添加重边,要注意判断
题目
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:
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:
. . . 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
Sample Output
思路:一个判定拓扑排序是否存在的问题.把9个小窗口应用看成9个点,然后对于4*4的棋盘来说: 看(1,2)这个方格当前放的是2数字,原本(1,2)方格我们能放的数有1和2两个数,现在放了2,说明从1应用->2应用有一条有向边.(也即是说应用1应先放,应用2后放,所以应用2能遮盖应用1).
现在来看(2,2)这个方格,这个方格是5,但是这个方格原先能放的应用有:1,2,4,5 四个应用.现在出现的应用是5,说明5是最后才放的应用.所以我们可知从应用1->5应用有一条有向边,从2->5,4->5都有一条有向边.
Trick:程序中有可能添加重边,要注意判断
#include<cstdio> #include<cstring> #include<iostream> using namespace std; int map[15][15]; int indegree[12]; int dir[4][2]= {0,0, 1,0, 0,1, 1,1}; int local[10][2]= {-1,-1, 0,0, 0,1, 0,2, 1,0, 1,1, 1,2, 2,0, 2,1, 2,2}; // 1~9号窗口的固定位置 int screen[5][5]; int topsort() { int i,j,loc,flag; for(i=1; i<=9; i++) { flag=0; for(j=1; j<=9; j++) if(indegree[j]==0) { flag=1; loc=j; break; } if(flag==0) return 0; indegree[loc]=-1; for(j=1; j<=9; j++) if(map[loc][j]) indegree[j]--; } return 1; } int main() { char str[20]; int i,j,k; int x,y,s; while(scanf("%s",str)&&strcmp(str,"ENDOFINPUT")!=0) { memset(map,0,sizeof(map)); memset(indegree,0,sizeof(indegree)); for(i=0; i<4; i++) for(j=0; j<4; j++) scanf("%d",&screen[i][j]); scanf("%s",str); for(k=1; k<=9; k++) { for(i=0; i<4; i++) { x=local[k][0]+dir[i][0]; y=local[k][1]+dir[i][1]; s=screen[x][y]; if(s!=k&&!map[k][s]) { map[k][s]=1; indegree[s]++; } } } if(topsort()) cout<<"THESE WINDOWS ARE CLEAN"<<endl; else cout<<"THESE WINDOWS ARE BROKEN"<<endl; } return 0; }
题目
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:
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
| ||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|
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 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
相关文章推荐
- 编译spark1.6.0出现 Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.4.0:exec (sparkr-pkg)
- 五子棋AI算法第二篇-极大极小值搜索算法
- USACO snail
- 杭电1021Fibonacci Again
- canvas和paint得简单应用
- BZOJ 3262 陌上花开、HDU 5618 Jam's problem again(三维偏序、cdq分治 + BIT)
- rails 分页插件 Kaminari 的 数组分页
- 【Elasticsearch】打分策略详解与explain手把手计算
- 解决'libproxychains.so.3' from LD_PRELOAD cannot be preloaded问题
- mongodb的 failindexkeytoolong
- HUD 1022-Train Problem I
- 真机测试libUMSocial_Sdk_4.2.2.a(UMSShareListController.o)' does not contain bitcode.
- flash air中读取本地文件的三种方法
- 解决Failed to load class "org.slf4j.impl.StaticLoggerBinder"
- HDU 1082 Matrix Chain Multiplication
- What is a fully qualified domain name (FQDN)?
- Failed to initialize monitor Thread: Unable to establish loopback connection解决方法
- pair模板类
- hdu5618 Jam's problem again(cdq分治)(BestCoder Round #70)
- MyBaits关联查询一对一、一对多