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
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111;
const int d[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n, m, k, cx[N], cy[N], dot, num[N][N];
bool mat[N][N], vis[N];
bool G[N][N];
void init()
{
memset(G, 0, sizeof(G));
for(int j = 0; j <= m+1; j++) {
mat[0][j] = 1;
mat[n+1][j] = 1;
}
for(int i = 0; i <= n+1; i++) {
mat[i][0] = 1;
mat[i][m+1] = 1;
}
}
void build()
{
for(int x = 1; x <= n; x++) {
for(int y = 1; y <= m; y++) if((x+y)&1) {
if(!mat[x][y]) {
for(int dir = 0; dir < 4; dir++) {
int nx = x + d[dir][0], ny = y + d[dir][1];
if(!mat[nx][ny]) {
G[num[x][y]][num[nx][ny]] = 1;
}
}
}
}
}
}
int dfs(int u)
{
for(int v = 0; v < dot; v++) {
if(G[u][v] && !vis[v]) {
vis[v] = true;
if(cy[v] == -1 || dfs(cy[v])) {//当前v没有匹配或者可以为其配偶找到其他匹配
cy[v] = u;
cx[u] = v;
return 1;
}
}
}
return 0;
}
int match()
{
int ret = 0;
memset(cx, -1, sizeof(cx));
memset(cy, -1, sizeof(cy));
for(int i = 0; i < dot; i++) {
if(cx[i] == -1) {
memset(vis, 0, sizeof(vis));
ret += dfs(i);
}
}
return ret ;
}
int main()
{
#ifdef J_Sure
freopen("000.in", "r", stdin);
freopen("999.out", "w", stdout);
#endif
while(scanf("%d%d", &n, &m), n||m) {
memset(mat, 0, sizeof(mat));
init();
scanf("%d", &k);
int x, y;
for(int i = 0; i < k; i++) {
scanf("%d%d", &x, &y);
mat[x][y] = 1;
}
dot = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(!mat[i][j]) {
num[i][j] = dot++;
}
}
}
build();
int ans = match();
printf("%d\n", ans);
}
return 0;
}
前向星代码如下:
/*
ID: j.sure.1
PROG:
LANG: C++
*/
/****************************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <string>
#include <climits>
#include <iostream>
#define LL long long
using namespace std;
const int INF = 0x3f3f3f3f;
/****************************************/
const int N = 111, M = 11111;
const int d[][2] = {-1, 0, 0, 1, 1, 0, 0, -1};
int n, m, k, tot, head[N], cou[N], num[N][N], nx, ny;
bool mat[N][N], vis[N];
struct Edge {
int v, next;
Edge(){}
Edge(int _v, int _next):
v(_v), next(_next){}
}edge[M];
void init()
{
tot = 0;
memset(head, -1, sizeof(head));
for(int j = 0; j <= m+1; j++) {
mat[0][j] = 1;
mat[n+1][j] = 1;
}
for(int i = 0; i <= n+1; i++) {
mat[i][0] = 1;
mat[i][m+1] = 1;
}
}
void add(int u, int v)
{
edge[tot] = Edge(v, head[u]);
head[u] = tot++;
}
void build()
{
for(int x = 1; x <= n; x++) {
for(int y = 1; y <= m; y++) if((x+y) & 1) {
if(!mat[x][y]) {
for(int dir = 0; dir < 4; dir++) {
int nx = x + d[dir][0], ny = y + d[dir][1];
if(!mat[nx][ny]) {
add(num[x][y], num[nx][ny]);
}
}
}
}
}
}
int dfs(int u)
{
for(int i = head[u]; ~i; i = edge[i].next) {
int v = edge[i].v;
if(!vis[v]) {
vis[v] = true;
if(cou[v] == -1 || dfs(cou[v])) {//当前v没有匹配或者可以为其配偶找到其他匹配
cou[v] = u;
return 1;
}
}
}
return 0;
}
int match()
{
int ret = 0;
memset(cou, -1, sizeof(cou));
for(int i = 0; i < nx; i++) {
memset(vis, 0, sizeof(vis));
ret += dfs(i);
}
return ret;
}
int main()
{
#ifdef J_Sure
// freopen("000.in", "r", stdin);
// freopen(".out", "w", stdout);
#endif
while(scanf("%d%d", &n, &m), n||m) {
memset(mat, 0, sizeof(mat));
init();
scanf("%d", &k);
int x, y;
for(int i = 0; i < k; i++) {
scanf("%d%d", &x, &y);
mat[x][y] = 1;
}
nx = ny = 0;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= m; j++) {
if(!mat[i][j]) {
if((i+j) & 1) num[i][j] = nx++;
else num[i][j] = ny++;
}
}
}
build();
int ans = match();
printf("%d\n", ans);
}
return 0;
}