HDU 5093 Battle ships (最大匹配)

来源:互联网 发布:玄幻网络四大名著 编辑:程序博客网 时间:2024/05/20 11:46

题目地址:点击打开链接


题意:给出一个n行m列的图,*代表海域,o代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互

到,之间只要有山就不能看到,问最多能放多少船。


思路:和HDU1045一样,把每个横向“块”看做二部图中的X中的顶点,竖向“块”看做集合中Y的顶点,若两个“块”有公

共的顶点海水,于是就连一条边。这样就转换成了没有公共顶点的最大边集,即最大匹配。


#include<iostream>#include<cstdio>#include<cstring>#include<vector>using namespace std;const int maxn = 55;vector<int> g[maxn*maxn];char pic[maxn][maxn];int row, col, num1, num2, rowNum[maxn][maxn], colNum[maxn][maxn], match[maxn*maxn];bool vis[maxn*maxn];bool dfs(int x){    for(int i = 0; i < g[x].size(); i++)    {        int v = g[x][i];        if(!vis[v])        {            vis[v] = 1;            if(match[v] == -1 || dfs(match[v]))            {                match[v] = x;                return 1;            }        }    }    return 0;}int Hungary(){    int res = 0;    for(int i = 1; i < num1; i++)    {        memset(vis, 0, sizeof(vis));        res += dfs(i);    }    return res;}int main(void){    int t;    cin >> t;    while(t--)    {        memset(match, -1, sizeof(match));        memset(rowNum, 0, sizeof(rowNum));        memset(colNum, 0, sizeof(colNum));        for(int i = 0; i < maxn*maxn; i++)            g[i].clear();        scanf("%d%d", &row, &col);        for(int i = 1; i <= row; i++)            for(int j = 1; j <= col; j++)                scanf(" %c", &pic[i][j]);        num1 = 1, num2 = 1;        for(int i = 1; i <= row; i++)            for(int j = 1; j <= col; j++)            {                if(pic[i][j] == '*' && !rowNum[i][j])                {                    for(int k = j; k <= col && pic[i][k] != '#'; k++)                        rowNum[i][k] = num1;                    num1++;                }                if(pic[i][j] == '*' && !colNum[i][j])                {                    for(int k = i; k <= row && pic[k][j] != '#'; k++)                        colNum[k][j] = num2;                    num2++;                }            }        for(int i = 1; i <= row; i++)            for(int j = 1; j <= col; j++)                if(pic[i][j] == '*')                    g[rowNum[i][j]].push_back(colNum[i][j]);        printf("%d\n", Hungary());    }    return 0;}


Battle ships

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1338    Accepted Submission(s): 501


Problem Description
Dear contestant, now you are an excellent navy commander, who is responsible of a tough mission currently.

Your fleet unfortunately encountered an enemy fleet near the South Pole where the geographical conditions are negative for both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredictable and incontrollable. 

But, fortunately, as an experienced navy commander, you are able to take opportunity to embattle the ships to maximize the utility of cannons on the battleships before the engagement. 

The target is, arrange as many battleships as you can in the map. However, there are three rules so that you cannot do that arbitrary:

A battleship cannot lay on floating ice
A battleship cannot be placed on an iceberg

Two battleships cannot be arranged in the same row or column, unless one or more icebergs are in the middle of them.
 

Input
There is only one integer T (0<T<12) at the beginning line, which means following T test cases.

For each test case, two integers m and n (1 <= m, n <= 50) are at the first line, represents the number of rows and columns of the battlefield map respectively. Following m lines contains n characters iteratively, each character belongs to one of ‘#’, ‘*’, ‘o’, that symbolize iceberg, ordinary sea and floating ice.
 

Output
For each case, output just one line, contains a single integer which represents the maximal possible number of battleships can be arranged.
 

Sample Input
24 4*oooo###**#*ooo*4 4#****#****#*ooo#
 

Sample Output
35
 

1 0
原创粉丝点击