Professor Clumsey is going to give an important talk this afternoon. Unortunately, he is not a very tidy person and has put all his transparencies on one big heap. Before giving the talk, he has to sort the slides. Being a kind of minimalist, he wants to do this with the minimum amount of work possible.
The situation is like this. The slides all have numbers written on them according to their order in the talk. Since the slides lie on each other and are transparent, one cannot see on which slide each number is written.
Well, one cannot see on which slide a number is written, but one may deduce which numbers are written on which slides. If we label the slides which characters A, B, C, ... as in the figure above, it is obvious that D has number 3, B has number 1, C number 2 and A number 4.
Your task, should you choose to accept it, is to write a program that automates this process.
Input
The input consists of several heap descriptions. Each heap descriptions starts with a line containing a single integer n, the number of slides in the heap. The following n lines contain four integers xmin, xmax, ymin and ymax, each, the bounding coordinates of the slides. The slides will be labeled as A, B, C, ... in the order of the input.
This is followed by n lines containing two integers each, the x- and y-coordinates of the n numbers printed on the slides. The first coordinate pair will be for number 1, the next pair for 2, etc. No number will lie on a slide boundary.
The input is terminated by a heap description starting with n = 0, which should not be processed.
Output
For each heap description in the input first output its number. Then print a series of all the slides whose numbers can be uniquely determined from the input. Order the pairs by their letter identifier.
If no matchings can be determined from the input, just print the word none on a line by itself.
Output a blank line after each test case.
Sample Input
4
6 22 10 20
4 18 6 16
8 20 2 18
10 24 4 8
9 15
19 17
11 7
21 11
2
0 2 0 2
0 2 0 2
1 1
1 1
0
Sample Output
Heap 1
(A,4) (B,1) (C,2) (D,3)
Heap 2
none
题意:给你n张幻灯片的左上角坐标和右下角坐标,然后给出A,B,C.....等幻灯片的一个坐标,判断能否将所有幻灯片和其位置对应,如果能对应输出对应的顺序,不能输出none。
思路:二分图思想,先将每张幻灯片的坐标和幻灯片左上角坐标和右下角坐标判断这个坐标与每张打乱的幻灯片能否对应,构成一张对应图,然后先用匈牙利算法判断能否对应,如果能一一对应,每一条边进行消边,继续用匈牙利算法判断,如果不能达成完全,则该边唯一,输出该边,如果消边之后还能对应,说明无法完全匹配。
#include<stdio.h>
#include<string.h>
struct node{
int x1,x2,y1,y2;//结构体存储每张幻灯片的坐标
}s[10000];
int s1[2000][4];
int a[1000][1000],next[1000],book[1000],m,n;
int next1[1000];
int z(int x)//匈牙利算法
{
int i,j;
for(i=1;i<=n;i++)
{
if(book[i]==0&&a[x][i]==1)
{
book[i]=1;
if(next[i]==0||z(next[i]))
{
next[i]=x;
return 1;
}
}
}
return 0;
}
int main()
{
int i,j,k,t=1;
while(scanf("%d",&n),n!=0)
{
memset(a,0,sizeof(a));
memset(next,0,sizeof(next));
for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",&s[i].x1,&s[i].x2,&s[i].y1,&s[i].y2);//存储每张幻灯片坐标
}
for(i=1;i<=n;i++)
{
scanf("%d%d",&s1[i][1],&s1[i][2]);//存储幻灯片位置坐标
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(s1[i][1]>s[j].x1&&s1[i][1]<s[j].x2&&s1[i][2]>s[j].y1&&s1[i][2]<s[j].y2)
{//判断幻灯片位置坐标能在哪一张幻灯片上
a[i][j]=1;//构图
}
}
}
/* for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}*/
int sum=0;
for(i=1;i<=n;i++)//匈牙利算法先进行判断能否匹配
{
memset(book,0,sizeof(book));
if(z(i)==1)
sum++;
}
/* for(i=1;i<=n;i++)
printf("%d ",next[i]);
printf("\n");
printf("%d\n",sum);*/
int f=0;
printf("Heap %d\n",t++);
if(sum==n)//能匹配时进行分边
{
for(i=1;i<=n;i++)
{
next1[i]=next[i];//next数组记录了对应情况
}
for(i=1;i<=n;i++)
{
a[next1[i]][i]=0;//消边处理
int num=0;
memset(next,0,sizeof(next));
for(j=1;j<=n;j++)
{
memset(book,0,sizeof(book));
if(z(j)==1)
num++;
}
if(num!=n)//如果消掉一条边不能完全匹配证明该边唯一
{
if(f==1)//数据间注意空格
printf(" (%c,%d)",'A'+i-1,next1[i]);
else
printf("(%c,%d)",'A'+i-1,next1[i]);
f=1;
}
a[next1[i]][i]=1;//还原
}
}
if(f==0)
{
printf("none");
}
printf("\n\n");
}
return 0;
}