思路:对每个格子进行编号,然后在标记哪些格子是可以链接的,人后就是Hungary()的事情了;
这个应该是特判的;
/***************************************** Author :Crazy_AC(JamesQi) Time :2015 File Name : *****************************************/ // #pragma comment(linker, "/STACK:1024000000,1024000000") #include <iostream> #include <algorithm> #include <iomanip> #include <sstream> #include <string> #include <stack> #include <queue> #include <deque> #include <vector> #include <map> #include <set> #include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> #include <limits.h> using namespace std; #define MEM(a,b) memset(a,b,sizeof a) #define pk push_back template<class T> inline T Get_Max(const T&a,const T&b){return a < b?b:a;} template<class T> inline T Get_Min(const T&a,const T&b){return a < b?a:b;} typedef long long ll; typedef pair<int,int> ii; const int inf = 1 << 30; const int INF = 0x3f3f3f3f; const int MOD = 1e9 + 7; const int maxn = 10010; int link[maxn]; int vis[maxn]; int ok[maxn]; int n,m; int total; bool dfs(int u){ int k; k = u + 1; if (u % m + 1 < m && k < total && ok[k] != -1 && !vis[k]){ vis[k] = 1; if (link[k] == -1 || dfs(link[k])){ link[k] = u; return true;; } } k = u + m; if (k < total && ok[k] != -1 && !vis[k]){ vis[k] = 1; if (link[k] == -1 || dfs(link[k])){ link[k] = u; return true; } } k = u - 1; if (u % m > 0 && k >= 0 && ok[k] != -1 && !vis[k]){ vis[k] = 1; if (link[k] == -1 || dfs(link[k])){ link[k] = u; return true; } } k = u - m; if (k >= 0 && ok[k] != -1 && !vis[k]){ vis[k] = 1; if (link[k] == -1 || dfs(link[k])){ link[k] = u; return true; } } return false; } inline int Hungary(){ int ret = 0; for (int i = 0;i < total;++i){ if (ok[i] != -1){ MEM(vis, 0); if (dfs(i)) ret++; } } return ret; } int main() { // ios::sync_with_stdio(false); // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); while(~scanf("%d%d",&n,&m)){ if (n == 0 && m == 0) break; MEM(link, -1); total = n * m; for (int i = 0;i < total;i++) ok[i] = i; int x,y; int t; scanf("%d",&t); while(t--){ scanf("%d%d",&x,&y); int tmp = (x - 1) * m + (y - 1); ok[tmp] = -1; // cout << "ok\n"; } int ans = Hungary(); // cout << ans << endl; printf("%d\n",ans / 2); MEM(vis, 0); for (int i = 0;i < total;i++){ if (ok[i] != -1 && link[i] != -1 && !vis[link[i]]&& !vis[i]){ vis[link[i]] = vis[i] = 1; printf("(%d,%d)--(%d,%d)\n",i/m + 1,i%m + 1,link[i]/m + 1,link[i]%m + 1); } } } return 0; }