题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?
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 produce one line of output, containing an integer value representing the maximum number of properties which can be sold.
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
3
Source: South America 2002, Practice
题意:
有N * N 的土地,当中某些点被挖成池塘了。其余的地方为空地,
如今要分成 1 * 2 的空地来出售,
求最大能出售的数量。
PS:
把每块土地和他的上下左右建边!
代码例如以下:
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> using namespace std; //顶点编号从1開始的 #define MAXN 117 int LN, RN;//L,R数目 int g[MAXN][MAXN], linker[MAXN]; bool used[MAXN]; int vis[MAXN][MAXN], a[MAXN][MAXN]; int dirx[4] = {0,0,1,-1}; int diry[4] = {1,-1,0,0}; int dfs(int L)//从左边開始找增广路径 { int R; for(R = 1; R <= RN; R++) { if(g[L][R]!=0 && !used[R]) { //找增广路。反向 used[R]=true; if(linker[R] == -1 || dfs(linker[R])) { linker[R]=L; return 1; } } } return 0; } int hungary() { int res = 0 ; int L; memset(linker,-1,sizeof(linker)); for( L = 1; L <= LN; L++) { memset(used,0,sizeof(used)); if(dfs(L) != 0) res++; } return res; } void init() { memset(a,0,sizeof(a)); memset(vis,0,sizeof(vis)); memset(g,0,sizeof(g)); } int main() { int n, m; int L, R; int k; while(~scanf("%d%d",&n,&m)) { if(n==0 && m==0) break; init(); scanf("%d",&k); int x, y; int cont = 1; for(int i = 1; i <= k; i++) { scanf("%d%d",&x,&y); vis[x][y] = 1;//池塘 } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(!vis[i][j]) { a[i][j] = cont++; } } } for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(!vis[i][j]) { for(int h = 0; h < 4; h++) { int tx = i+dirx[h]; int ty = j+diry[h]; if(tx>=1&&tx<=n && ty>=1&&ty<=m && !vis[i][j]) { g[a[i][j]][a[tx][ty]] = 1; g[a[tx][ty]][a[i][j]] = 1; } } } } } LN = cont; RN = cont; int ans = hungary(); printf("%d\n",ans/2); } return 0 ; }