程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)

編輯:C++入門知識

hdu-----(1507)Uncle Tom's Inherited Land*(二分匹配)


Problem Description Your old uncle Tom inherited a piece of land from his great-great-uncle. Originally, the property had been in the shape of a rectangle. A long time ago, however, his great-great-uncle decided to divide the land into a grid of small squares. He turned some of the squares into ponds, for he loved to hunt ducks and wanted to attract them to his property. (You cannot be sure, for you have not been to the place, but he may have made so many ponds that the land may now consist of several disconnected islands.)   Your uncle Tom wants to sell the inherited land, but local rules now regulate property sales. Your uncle has been informed that, at his great-great-uncle's request, a law has been passed which establishes that property can only be sold in rectangular lots the size of two squares of your uncle's property. Furthermore, ponds are not salable property.   Your uncle asked your help to determine the largest number of properties he could sell (the remaining squares will become recreational parks).          Input Input will include several test cases. The first line of a test case contains two integers N and M, representing, respectively, the number of rows and columns of the land (1 <= N, M <= 100). The second line will contain an integer K indicating the number of squares that have been turned into ponds ( (N x M) - K <= 50). Each of the next K lines contains two integers X and Y describing the position of a square which was turned into a pond (1 <= X <= N and 1 <= Y <= M). The end of input is indicated by N = M = 0.       Output For each test case in the input your program should first output one line, containing an integer p representing the maximum number of properties which can be sold. The next p lines specify each pair of squares which can be sold simultaneity. If there are more than one solution, anyone is acceptable. there is a blank line after each test case. See sample below for clarification of the output format.       Sample Input 4 4 6 1 1 1 4 2 2 4 1 4 2 4 4 4 3 4 4 2 3 2 2 2 3 1 0 0       Sample Output 4 (1,2)--(1,3) (2,1)--(3,1) (2,3)--(3,3) (2,4)--(3,4) 3 (1,1)--(2,1) (1,2)--(1,3) (2,3)--(3,3)       Source South America 2002 - Practice   這道題,怎麼說呢,比較的抽象,就是將一個點作為一個匹配的對象來處理,比較容易想到最大匹配,至於這個點的坐標該如何來存放 可以開四維的數組,貌似這樣會到10e8+的數據規模 達到250M的樣子,所以放棄這個念頭...轉而用結構體來定義自己的變量 然後就是一個一如既往的匈牙利算法啦,但是在遍歷所有的坐標時,雖然可以求出正確的匹配數,貌似在輸出那個誰匹配誰的時候,又不好處理 比如  你在 (1,2)這個點的時候,假如你和(1,3)匹配了,那麼在(1,3)那個點時,又會有(1,3)匹配(1,2)這樣就不好處裡了... 於是呼,你會發現這個地方的匹配有些奇特,那就是貌似他臨近的點的坐標的和總和所在點呈現奇偶互異的狀態,所以我們不妨這樣搞,取這個點就不在取那個點 就這樣跳躍著,取點,然後就可以了....這樣的話,一切就都ok了.... 代碼: 復制代碼  1 #include<cstring>  2 #include<cstdio>  3 #include<cstdlib>  4 #define init(a) memset(a,0,sizeof(a))  5 const int maxn=105;  6 bool map[maxn][maxn];  7 bool vis[maxn][maxn];  8 struct node{  9   int x,y; 10 }; 11 node mat[maxn][maxn]; 12 int n,m; 13 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}}; 14 int match(node aa){ 15  16       node tem; 17      if(!map[aa.x][aa.y]){ 18       for(int ll=0;ll<4;ll++)  //掃描它的四周... 19       { 20         tem=(node){aa.x+dir[ll][0],aa.y+dir[ll][1]}; 21         if(tem.y>0&&tem.x>0&&tem.x<=n&&tem.y<=m&&!map[tem.x][tem.y]&&!vis[tem.x][tem.y]){ 22              vis[tem.x][tem.y]=1; 23              if(!mat[tem.x][tem.y].x||match(mat[tem.x][tem.y])){ 24               mat[tem.x][tem.y]=aa; 25               return 1; 26             } 27           } 28         } 29        } 30  return 0; 31 } 32 int main() 33 { 34     int k,x,y; 35     //freopen("test.in","r",stdin); 36     while(scanf("%d%d",&n,&m)!=EOF,n+m){ 37      scanf("%d",&k); 38      init(map); 39     while(k--){ 40          scanf("%d%d",&x,&y); 41           map[x][y]=1;  //代表一個池塘 42     } 43     init(mat); 44     int ans=0; 45     for(int i=1;i<=n;i++){ 46    for(int j=1;j<=m;j++){ 47       if(((i+j)&1)){  //不娶相鄰的點...取奇數或者偶數都一樣,, 48       init(vis); 49       ans+=match((node){i,j}); 50       } 51    } 52    } 53    printf("%d\n",ans); 54       for(int i=1;i<=n;i++){ 55    for(int j=1;j<=m;j++){ 56        if(mat[i][j].x){ 57            printf("(%d,%d)--(%d,%d)\n",i,j,mat[i][j].x,mat[i][j].y); 58        } 59    } 60    } 61   } 62   return 0; 63 }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved