Uncle Tom's Inherited Land* 6.3.7

Uncle Tom's Inherited Land*

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 147    Accepted Submission(s): 96

 
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)
 
问题的意思是选择两个相邻的块绑在一起出售,这其中不包括黑色的块。
可以看到(i, j)相邻的就是上下左右这四个块。左右两边的奇偶性是不同的。因此二分,求最大匹配。
 
 
      
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;

const int MAXN = 10010;

int n, m;
int g[MAXN], link[MAXN], done[MAXN];
int dir[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
vector<int> G[MAXN];
bool judge(int x, int y){
	if(x < 1 || x > n || y < 1 || y > m || g[(x-1)*m+y] == -1) return false;
	return true;
}

bool find_match(int k){
	for(int i = 0; i < G[k].size(); i++){
		int to = G[k][i];
		if(!done[to]){
			done[to] = 1;
			if(link[to] == -1 || find_match(link[to])){
				link[to] = k;
				return true;
			} 
		}
	}
	return false;
}
int main(){
	int k, a, b;
	while(~scanf("%d%d", &n, &m) && (n || m)){
		memset(g, 0, sizeof(g));
		memset(G, 0, sizeof(G));	
		scanf("%d", &k);
		for(int i = 0; i < k; i++){
			scanf("%d%d", &a, &b);
			g[m * (a-1) + b] = -1;
		}
		int nx, ny;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= m; j++){
				if(g[(i-1)*m+j] == 0 && (i + j) % 2 == 0){
					for(int k = 0; k < 4; k++){
						nx = i + dir[k][0];
						ny = j + dir[k][1];
						if(!judge(nx, ny)) continue;
						else G[(i - 1) * m + j].push_back((nx - 1) * m + ny);	
					}
				}
			}
		}
		int cnt = 0;
		memset(link, -1, sizeof(link));
		for(int i = 1; i <= n*m; i++){
			memset(done, 0, sizeof(done));
			if(find_match(i)) cnt++;
		}
		printf("%d\n", cnt);
		int x1, y1, x2, y2;
		for(int i = 1; i <= n*m; i++){
			if(link[i] != -1){
				x1 = i / m ;
				y1 = i % m;
				x2 = link[i] / m;
				y2 = link[i] % m;
				if(y1 == 0) y1 = m;
				else x1++;
				if(y2 == 0) y2 = m;
				else x2++;
				printf("(%d,%d)--(%d,%d)\n", x2, y2, x1, y1);
			}
		}
	}	
	return 1;
}

猜你喜欢

转载自blog.csdn.net/bruce_teng2011/article/details/38589643
今日推荐