相邻空格连边,求最大匹配。
#include <cmath> #include <cstdlib> #include <cstdio> #include <cstring> #include <algorithm> #include <fstream> #include <iostream> #define rep(i, l, r) for(int i=l; i<=r; i++) #define clr(x, c) memset(x, c, sizeof(x)) #define N 109 #define MAX 1<<30 #define ll long long using namespace std; int read() { int x=0, f=1; char ch=getchar(); while (ch<'0' || ch>'9') { if (ch=='-') f=-1; ch=getchar(); } while (ch>='0' && ch<='9') { x=x*10+ch-'0'; ch=getchar(); } return x*f; } struct edge{int y1, y2, n;} e[N*N]; int fir[N][N], en; int n, m, k1[N][N], k2[N][N], ans; bool b[N][N], map[N][N]; void Add(int x1, int x2, int y1, int y2) { en++, e[en].y1=y1, e[en].y2=y2, e[en].n=fir[x1][x2], fir[x1][x2]=en; } bool Find(int x1, int x2) { int o=fir[x1][x2], y1=e[o].y1, y2=e[o].y2; while (o) { if (!b[y1][y2]) { b[y1][y2]=1; if (!k1[y1][y2] || Find(k1[y1][y2], k2[y1][y2])) { k1[y1][y2]=x1; k2[y1][y2]=x2; return true; } } o=e[o].n, y1=e[o].y1, y2=e[o].y2; } return false; } int main() { while (scanf("%d%d", &n, &m) && (n || m)) { int x, y, q=read(); ans=en=0; clr(fir, 0); clr(k1, 0); clr(k2, 0); clr(map, 1); rep(i, 1, q) map[x=read()][y=read()]=0; rep(i, 1, n) rep(j, 1, m) if (map[i][j]) { if (i-1>0 && map[i-1][j]) Add(i, j, i-1, j); if (j-1>0 && map[i][j-1]) Add(i, j, i, j-1); if (i+1<=n && map[i+1][j]) Add(i, j, i+1, j); if (j+1<=m && map[i][j+1]) Add(i, j, i, j+1); } rep(i, 1, n) rep(j, 1, m) if ((i+j)%2 && map[i][j]) { clr(b, 0); if (Find(i, j)) ans++; } printf("%d ", ans); rep(i, 1, n) rep(j, 1, m) if ((i+j)%2==0 && k1[i][j]) { printf("(%d,%d)--(%d,%d) ", k1[i][j], k2[i][j], i, j); } printf(" "); } return 0; }