Antenna Placement poj3020

來源:互聯網
上載者:User

    這是一道比較經典的二分匹配,將格子按黑白標記後,再將含'*'的格子按黑白分成兩組,建邊。最後的結果就是總的'*'的個數-匹配數。

#include<iostream>#include<cstdio>#include<vector>using namespace std;vector<int>map[400];//按奇偶性建二分圖 int t,n,m;int match[400],fy[400],ln,rn; int map1[41][11];//原始圖 int dirt[4][2]={0,1,0,-1,1,0,-1,0};int path(int u){for(int i=0,j;i<map[u].size();i++){j=map[u][i];if(!fy[j]){fy[j]=1;if(match[j]==-1||path(match[j])){match[j]=u;return 1;}}}return 0;}int main(){int i,j,x,y,ans;char tmp;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);ln=rn=0;for(i=1;i<=n;i++){getchar();for(j=1;j<=m;j++){scanf("%c",&tmp);if(tmp=='*'){if((i+j)&1)//奇點 map1[i][j]=++ln;else//偶點 map1[i][j]=++rn;}elsemap1[i][j]=0;}}for(i=1;i<=ln;i++)map[i].clear();for(i=1;i<=rn;i++)match[i]=-1;//建二分圖 for(i=1;i<=n;i++)for(j=1;j<=m;j++){if(!map1[i][j]||!((i+j)&1))continue;for(int k=0;k<4;k++){x=i+dirt[k][0];y=j+dirt[k][1];if(x<=0||x>n||y<=0||y>m||!map1[x][y])continue;map[map1[i][j]].push_back(map1[x][y]);}}//求最大匹配 ans=0;for(i=1;i<=ln;i++){for(j=1;j<=rn;j++)fy[j]=0;if(path(i))ans++;}printf("%d\n",ln+rn-ans);}return 0;}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.