题意:给定n*m的矩阵,z个点,下面z个点坐标表示无法铺设,问最多铺设几个1*2的方格,并输出这些方格的匹配边。
思路:二分匹配,每一个匹配表示2个坐标相连,2个坐标相连就是这2个坐标组成一个1*2的方格,坐标一维化
链接:hdu 1507
#include #include #include #include #include #include #include #include #include #include #define INF 0x3f3f3f3f using namespace std; const int inf = 0x3f3f3f3f; const int maxn = 1005; int used[maxn]; int link[maxn]; int mat[maxn][maxn]; int gn, gm; int ma[maxn][maxn]; int mb[maxn]; //int x[maxn], y[maxn]; int dfs(int t) { for(int i = 1; i <= gm; i++) { if(!used[i] && mat[t][i] == 1) { used[i] = 1; if(link[i] == -1 || dfs(link[i])) { link[i] = t; return 1; } } } return 0; } int maxmatch() { int num = 0; memset(link, 0xff, sizeof(link)); for(int i = 1; i <= gn; i++) { memset(used, 0, sizeof(used)); if(dfs(i)) { num++; } } return num; } int main() { int n, m, k, t, kcase = 0; int x, y; while(~scanf("%d %d", &n, &m) && n && m){ int mm = m * n; memset(mat, 0, sizeof(mat)); memset(ma, 1, sizeof(ma)); scanf("%d", &k); for(int i = 1; i <= k; i++) { scanf("%d %d", &x, &y); ma[x][y] = 0; } gn = gm = 0; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if(ma[i][j]) { ma[i][j] = ++gn; mb[gn] = i * mm + j; } } } gm = gn; for(int i = 1; i <= n; i++) { for(int j = 1; j <= m; j++) { if((i + j) % 2 && ma[i][j]) { if(i > 1 && ma[i - 1][j]) { mat[ma[i][j]][ma[i - 1][j]] = 1; } if(j > 1 && ma[i][j - 1]) { mat[ma[i][j]][ma[i][j - 1]] = 1; } if(j < m && ma[i][j + 1]) { mat[ma[i][j]][ma[i][j + 1]] = 1; } if(i < n && ma[i + 1][j]) { mat[ma[i][j]][ma[i + 1][j]] = 1; } } } } int ans = maxmatch(); printf("%d\n", ans); for(int i = 1; i <= gn; i++) { if(link[i] != -1) { int x1 = mb[i] / mm; int y1 = mb[i] % mm; int x2 = mb[link[i]] / mm; int y2 = mb[link[i]] % mm; printf("(%d,%d)--(%d,%d)\n", x1, y1, x2, y2); } } puts(""); } return 0; }