/* THE PROGRAM IS MADE BY PYY */
/*----------------------------------------------------------------------------//
Copyright (c) 2011 panyanyany All rights reserved.
URL : http://acm.hdu.edu.cn/showproblem.php?pid=1507
Name : 1507 Uncle Tom's Inherited Land*
Date : Friday,August 27, 2011
Time Stage : 3 hours
Result:
4504524 2011-08-27 21:30:42 Accepted 1507
31MS 420K 2051 B
C++ pyy
Test Data:
Review:
一开始犯了个很白痴的错误,结果做了几个小时……
若将棋盘中相邻的格子分别用黑白染色的话,则同一种颜色的格子必然会出现:
(x+y) 为奇数的情况,所以通过(x+y)&1是否为可以判断是否同一种颜色
//----------------------------------------------------------------------------*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define FALSE 0
#define TRUE 1
#define infinity 0x0f0f0f0f
#define minus_inf 0x80808080
#define MAXSIZE 109
#define SafeAccess(x, y) ((0 < (x) && (x) <= n) && (0 < (y) && (y) <= m))
int n, m, k ;
int map[MAXSIZE][MAXSIZE], cover[MAXSIZE][MAXSIZE] ;
int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}} ;
typedef struct tagNODE {
int x, y ;
} NODE ;
NODE link[MAXSIZE][MAXSIZE] ;
int find (int x, int y)
{
int i, j, k ;
for (k = 0 ; k < 4 ; ++k)
{
i = x + dir[k][0] ;
j = y + dir[k][1] ;
if (SafeAccess (i, j) && !map[i][j] && cover[i][j] == FALSE)
{
cover[i][j] = TRUE ;
if (link[i][j].x == 0 || find (link[i][j].x, link[i][j].y))
{
link[i][j].x = x ;
link[i][j].y = y ;
return 1 ;
}
}
}
return 0 ;
}
int main ()
{
int i, j, x, y, sum ;
while (scanf ("%d%d", &n, &m), n | m)
{
memset (map, 0, sizeof (map)) ;
scanf ("%d", &k) ;
for (i = 1 ; i <= k ; ++i)
{
scanf ("%d%d", &x, &y) ;
map[x][y] = 1 ;
}
sum = 0 ;
memset (link, FALSE, sizeof (link)) ;
// 从白色格子开始找
for (i = 1 ; i <= n ; ++i)
for (j = 1 ; j <= m ; ++j)
if (!map[i][j] && (i+j)&1)
{
memset (cover, FALSE, sizeof (cover)) ;
// 找黑色格子的匹配项
sum += find (i, j) ;
}
printf ("%d\n", sum) ;
for (i = 1 ; i <= n ; ++i)
for (j = 1 ; j <= m ; ++j)
if (link[i][j].x)
{
printf ("(%d,%d)--(%d,%d)\n", i, j, link[i][j].x, link[i][j].y) ;
}
}
return 0 ;
}
杭电 hdu 1507 Uncle Tom‘s Inherited Land*(二分图,最大匹配)
最新推荐文章于 2024-04-23 09:54:18 发布