您的位置:首页 > 大数据 > 人工智能

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:程序中有可能添加重边,要注意判断

#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:

11..
11..
....
....
.22.
.22.
....
....
..33
..33
....
....
....
44..
44..
....
....
.55.
.55.
....
....
..66
..66
....
....
....
77..
77..
....
....
.88.
.88.
....
....
..99
..99
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:
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

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: