Bnuoj 2345 muddy fields

Source: Internet
Author: User
Muddy fieldstime limit: 1000 msmemory limit: 65536 kbthis problem will be judged on PKU. Original ID: 2226
64-bit integer Io format: % LLD Java class name: Main rain has pummeled the cows 'field, a rectangular grid of R rows and C columns (1 <= r <= 50, 1 <= C <= 50 ). while good for the grass, the rain makes some patches of bare earth quite muddy. the cows, being meticulous grazers, don't want to get their hooves dirty while they eat.

To prevent those muddy hooves, Farmer John will place a number of wooden boards over the muddy parts of the cows 'field. each of the Boards is 1 unit wide, and can be any length long. each board must be aligned parallel to one of the sides of the field.

Farmer John wishes to minimize the number of boards needed to cover the muddy spots, some of which might require more than one board to cover. the Boards may not cover any grass and deprive the cows of grazing area but they can overlap each other.

Compute the minimum number of boards FJ requires to cover all the mud in the field. Input * Line 1: two space-separated integers: R and C

* Lines 2 .. R + 1: Each line contains a string of C characters, with '*' representing a muddy patch, and '. 'representing a grassy patch. no spaces are present. output * Line 1: A single integer representing the number of boards FJ needs. sample Input
4 4*.*..******...*.
Sample output
4
Hintoutput details:

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

Problem solving: Find the least point coverage, that is, find the maximum matching. Mark the continuous water blocks of each row as a vertex set. Mark the continuous water blocks of each column as a vertex set, then, each cell uses its row block label and column block label as the endpoint to create an edge. Create a bipartite graph and find the maximum match.

 

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #define LL long long13 #define INF 0x3f3f3f3f14 using namespace std;15 char mp[60][60];16 int row,col;17 int r[60][60],c[60][60];18 vector<int>g[6000];19 int lik[6000],cnt;20 bool used[6000];21 void init(){22     int i,j;23     char pre = ‘-‘;24     cnt = 1;25     memset(r,0,sizeof(r));26     memset(c,0,sizeof(c));27     for(i = 0; i < row; i++){28         for(j = 0; j < col; ){29             if(mp[i][j] == ‘*‘){30                 while(j < col && mp[i][j] == ‘*‘) {r[i][j] = cnt;j++;}31                 cnt++;32             }else j++;33         }34     }35     for(i = 0; i < col; i++){36         for(j = 0; j < row; ){37             if(mp[j][i] == ‘*‘){38                 while(j < row && mp[j][i] == ‘*‘){c[j][i] = cnt;j++;}39                 cnt++;40             }else j++;41         }42     }43 }44 bool dfs(int u){45     for(int i = 0; i < g[u].size(); i++){46         if(!used[g[u][i]]){47             used[g[u][i]] = true;48             if(lik[g[u][i]] == -1 || dfs(lik[g[u][i]])){49                 lik[g[u][i]] = u;50                 return true;51             }52         }53     }54     return false;55 }56 int main(){57     int i,j,ans;58     while(~scanf("%d%d",&row,&col)){59         for(i = 0; i < row; i++)60             scanf("%s",mp[i]);61         for(i = 0; i < 6000; i++){62             g[i].clear();63             lik[i] = -1;64         }65         init();66         for(i = 0; i < row; i++){67             for(j = 0; j < col; j++){68                 if(mp[i][j] == ‘*‘){69                     g[r[i][j]].push_back(c[i][j]);70                     g[c[i][j]].push_back(r[i][j]);71                 }72             }73         }74         for(ans = 0,i = 1; i < cnt; i++){75             memset(used,false,sizeof(used));76             if(dfs(i)) ans++;77         }78         cout<<(ans>>1)<<endl;79     }80     return 0;81 }
View code

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.