zoukankan      html  css  js  c++  java
  • Sorting Slides(二分图匹配——确定唯一匹配边)

    题目描述:

    Professor Clumsey is going to give an important talk this afternoon. Unfortunately, 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

      1 /*
      2 题意描述:输入幻灯片的坐标位置,页码的坐标位置,将能唯一确定页码的幻灯片按照顺序输出,
      3 如果没有一张能够唯一确定则输出none
      4 */ 
      5 /*
      6 解题思路:基本思路是将幻灯片和页码进行匹配,按照二分图匹配的思路求最大匹配,如果某一条边
      7 是唯一边,则将这条边去掉以后,该二分图的最大匹配数将<n,输出该匹配边。
      8 */ 
      9 #include<stdio.h>
     10 #include<string.h>
     11 const int N=501;
     12 struct Slide{
     13     int xmin,xmax,ymin,ymax;
     14 }slide[N];
     15 struct Num{
     16     int x,y;
     17 }num[N];
     18 int n,e[N][N],cx[N],cy[N],bk[N]; 
     19 int maxmatch();
     20 int path(int u);
     21 
     22 int main()
     23 {
     24     int i,j,t=0;
     25     while(scanf("%d",&n), n != 0)
     26     {
     27         for(i=0;i<n;i++)
     28             scanf("%d%d%d%d",&slide[i].xmin,&slide[i].xmax,&slide[i].ymin,&slide[i].ymax);
     29         for(i=0;i<n;i++)
     30             scanf("%d%d",&num[i].x,&num[i].y);
     31             
     32         memset(e,0,sizeof(e));
     33         for(i=0;i<n;i++){
     34             for(j=0;j<n;j++){
     35                 if(num[i].x>slide[j].xmin && num[i].x<slide[j].xmax 
     36                 && num[i].y>slide[j].ymin && num[i].y<slide[j].ymax)
     37                 e[j][i]=1;//点i在j张里,按照输出顺序,先输出字母,再输出数字 
     38             }
     39         }    
     40         
     41         printf("Heap %d
    ",++t); 
     42         int flag=0;
     43         for(i=0;i<n;i++){
     44             for(j=0;j<n;j++){
     45                 if(e[i][j])
     46                 {
     47                     e[i][j]=0;
     48                     if(maxmatch() < n)
     49                     {
     50                         if(flag)
     51                         printf(" (%c,%d)",'A'+i,j+1); 
     52                         else 
     53                         printf("(%c,%d)",'A'+i,j+1); 
     54                         flag=1;
     55                     }
     56                     e[i][j]=1;
     57                 }
     58             }
     59         }
     60              
     61         if(flag)
     62         printf("
    
    ");
     63         else
     64         printf("none
    
    ");
     65     }
     66     return 0;
     67 }
     68 
     69 int maxmatch()
     70 {
     71     int i;
     72     memset(cx,-1,sizeof(cx));
     73     memset(cy,-1,sizeof(cy));
     74     int ans=0;
     75     for(i=0;i<n;i++)
     76     {
     77         if(cy[i]==-1)
     78         {
     79             memset(bk,0,sizeof(bk));
     80             ans += path(i);
     81         }    
     82     } 
     83     return ans;
     84 } 
     85 int path(int u)
     86 {
     87     int i;
     88     for(i=0;i<n;i++)
     89     {
     90         if(e[u][i] && !bk[i])
     91         {
     92             bk[i]=1; 
     93             if(cx[i]==-1 || path(cx[i]))
     94             {
     95                 cx[i]=u;
     96                 cy[u]=i;
     97                 return 1;
     98             }
     99         }
    100     }
    101     return 0;
    102 }
    103 /*易错分析:按照输出顺序,先幻灯片再页码,所以输入边的时候j在前,i在后*/ 
  • 相关阅读:
    python 运用numpy库与matplotlib库绘制数据图
    pil库的介绍与应用
    使用jieba库与wordcloud库第三方库进行词频统计
    将驼峰命名转为连字符格式
    数组去重的多种方法
    数字美化-pretty-number 将数字转换成k 、w
    npm方式开发的插件使用yarn link的方式引入到目标项目中,在目标项目中无法读取到Vue,vuex, vue-i18n的解决方法
    eslint配置
    webpack 中的 process.env
    类型“VueConstructor<Vue>”上不存在属性“install”。ts(2339)
  • 原文地址:https://www.cnblogs.com/wenzhixin/p/8428600.html
Copyright © 2011-2022 走看看