poj2226Muddy Fields【最小點覆蓋(建圖的思路比較好)】

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   ar   

大意:如告訴你一個矩陣,讓你用1 * x (x 為任意值) 的木板去鋪符號*  可以覆蓋

問最少多少木板能夠把所有的*覆蓋掉

4 4*.*..******...*.

Boards 1, 2, 3 and 4 are placed as follows: 
1.2. 
.333 
444. 
..2. 
Board 2 overlaps boards 3 and 4.

分析:
題目要求是把所有的點都覆蓋掉
回憶一下之前做過的
這種類型的大概就是二分圖的最小點覆蓋或者最小邊覆蓋
那麼怎麼建立二分圖呢
因為X是個不確定值
所以我們盡量讓X取值越大越好
也就是說每一個橫向或縱向的*地區都可以看做是一個聯通塊
那麼只要把橫向的連通塊放在左集合,把縱向的連通塊放在右集合 把點看左邊
那麼只要所有的邊被覆蓋則就是覆蓋掉所有的*了
這樣想來因為左右集合放的就是所求的連通塊,那麼只要求出最小點覆蓋集即ok啦
最小點覆蓋集 = 二分圖最大匹配

代碼:
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6  7 const int maxn = 55; 8 int G[maxn * maxn / 2][maxn * maxn / 2]; 9 10 int vis[maxn * maxn / 2];11 int Link[maxn * maxn / 2];12 int n;13 bool Find(int u) {14     for(int i = 1; i <= n; i++) {15         if(G[u][i] && !vis[i]) {16             vis[i] = 1;17             if(Link[i] == -1 || Find(Link[i])) {18                 Link[i] = u;19                 return true;20             }21         }22     }23     return false;24 }25 26 int solve() {27     int cnt = 0;28     memset(Link, -1, sizeof(Link));29     for(int i = 1; i <= n; i++) {30         memset(vis, 0, sizeof(vis));31         if(Find(i)) cnt++;32     }33     return cnt;34 }35 36 char mat[maxn][maxn];37 int vis1[maxn][maxn], vis2[maxn][maxn];38 int main() {39     int m;40     for(int i = 0; i <= 50; i++) {41         mat[0][i] = mat[i][0] = ‘.‘;42     }43     while(EOF != scanf("%d %d",&n, &m)) {44         for(int i = 1; i <= n; i++) {45             for(int j = 1; j <= m; j++) {46                 scanf("\n%c",&mat[i][j]);47             }48         }49         int tot1 = 1; int tot2 = 1;50         memset(vis1, 0, sizeof(vis1)); memset(vis2, 0, sizeof(vis2));51         for(int i = 1; i <= n; i++) {52             for(int j = 1; j <= m; j++) {53                 if(mat[i][j] == ‘*‘) {54                     if(!vis1[i][j - 1]) {55                         vis1[i][j] = tot1++;56                     } else {57                         vis1[i][j] = vis1[i][j - 1];58                     }59                     if(!vis2[i - 1][j]) {60                         vis2[i][j] = tot2++;61                     } else {62                         vis2[i][j] = vis2[i - 1][j];63                     }64                 }65             }66         }67         memset(G, 0, sizeof(G));68         for(int i = 1; i <= n; i++) {69             for(int j = 1; j <= m; j++) {70                 if(mat[i][j] == ‘*‘) {71                     G[vis1[i][j]][vis2[i][j]] = 1;72                 }73             }74         }75         n = max(tot1, tot2);76         printf("%d\n",solve());77     }78     return 0;79 }
View Code

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.