題意:
要在一個矩陣中放1*2的長方形..有些點不能放長方形.問最多可以放多少個....
題解:
這題我第一反應是狀態壓縮DP..但是看範圍.好吧..二分圖匹配..但是有個問題..如果直接的點對點的做邊..會出現混亂..並且不符合2分圖的基本模型(同側的點無直接的任何關係才行)...所以做二分圖..第一件事就是把點分成內部不會直接影響的兩堆..本題以(x+y)為奇數或偶數來分成兩堆..剩下的就很裸了..
Program:
#include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<stack>#include<queue>#define ll long long#define MAXN 105using namespace std;int n,match[MAXN],hash[MAXN][MAXN],w[MAXN*MAXN][2];bool arc[MAXN][MAXN],used[MAXN],s[MAXN][MAXN]; bool dfs(int x){ int i; for (i=1;i<=n;i++) if (arc[x][i] && !used[i]) { used[i]=true; if (!match[i] || dfs(match[i])) { match[i]=x; return true; } } return false; }int getmax(){ int sum=0; memset(match,0,sizeof(match)); for (int i=1;i<=n;i++) { memset(used,false,sizeof(used)); sum+=dfs(i); } return sum;}int main(){ int i,j,x,y,m,num,cases=0; while (~scanf("%d%d",&n,&m) && n) { memset(s,true,sizeof(s)); scanf("%d",&num); while (num--) { scanf("%d%d",&x,&y),s[x][y]=false; }; num=0; memset(hash,0,sizeof(hash)); for (x=1;x<=n;x++) for (y=1;y<=m;y++) if (s[x][y]) hash[x][y]=++num,w[num][0]=x,w[num][1]=y; memset(arc,false,sizeof(arc)); for (x=1;x<=n;x++) for (y=1;y<=m;y++) if ((x+y)%2 && s[x][y]) { if (x!=1 && s[x-1][y]) arc[hash[x][y]][hash[x-1][y]]=true; if (y!=1 && s[x][y-1]) arc[hash[x][y]][hash[x][y-1]]=true; if (x!=n && s[x+1][y]) arc[hash[x][y]][hash[x+1][y]]=true; if (y!=m && s[x][y+1]) arc[hash[x][y]][hash[x][y+1]]=true; } n=num; if (cases) printf("\n"); cases++; printf("%d\n",getmax()); for (i=1;i<=n;i++) if (match[i]) printf("(%d,%d)--(%d,%d)\n",w[match[i]][0],w[match[i]][1],w[i][0],w[i][1]); } return 0;}