zoj5093Battle ships【二分圖 棋盤覆蓋有斷點】

MissZhou要努力發表於2016-04-26

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

2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
 

Sample Output

3 5
 

rt,去年暑假的時候做過HDU 1281 棋盤遊戲 二分圖最大匹配 只允許每行每列放一個,只有一些點能放棋子,那麼很容易就能想到以這個座標行列連邊。

這個題不是了,不僅這樣,如果兩個可放點中間有'#'那麼這兩個點也可放,求最多放多少“棋子”。

最開始想的是,遍歷棋盤,每行、列可放的點最多個數作為與源點、匯點的連邊流量,中間依舊是連橫縱座標。示例資料弱,沒看出來問題,其實這麼做不對。

正確解法:用‘#’分割的不同區域分成新的行、列,連邊的序號是新的行、列號。二分圖可搞,原理:對於分割開的區域,一個區域只能放一個棋子,那麼就和原來的題一樣了==

#include <iostream>
#include<cstdio>
#include<cstring>
#include<map>
using namespace std;
#define M 550
#define MAXN 50*50
#define inf 0x3f3f3f3f
int uN,vN;//u,v數目
int g[MAXN][MAXN];
int linker[MAXN];
bool used[MAXN];
bool dfs(int u)//從左邊開始找增廣路徑
{
    int v;
    for(v=0;v<vN;v++)//這個頂點編號從0開始,若要從1開始需要修改
      if(g[u][v]&&!used[v])
      {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
          {//找增廣路,反向
              linker[v]=u;
              return true;
          }
      }
    return false;//這個不要忘了,經常忘記這句
}
int hungary()
{
   // puts("hungary");
    int res=0;
    int u;
    memset(linker,-1,sizeof(linker));
    for(u=0;u<uN;u++)
    {
        memset(used,0,sizeof(used));
        if(dfs(u)) res++;
    }
    return res;
}
char str[100][100];
int posrow[100][100],poscol[100][100];
int t,m,n;
int row[10000],col[10000],rownum,colnum;
int main()
{
   // freopen("cin.txt","r",stdin);
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&m,&n);
        for(int i=0;i<m;i++)scanf("%s",str[i]);
        rownum=colnum=1;
        for(int i=0;i<m;i++)
        {
            bool flag=1;
            for(int j=0;j<n;j++)
            {
                if(str[i][j]=='#'&&flag==0) rownum++,flag=1;
                if(str[i][j]=='*'&&flag) flag=0;
                posrow[i][j]=rownum;
            }
            if(flag==0) rownum++;
        }
       // rownum--;
        for(int i=0;i<n;i++)
        {
            bool flag=1;
            for(int j=0;j<m;j++)
            {
                if(str[j][i]=='#'&&flag==0) colnum++,flag=1;
                if(str[j][i]=='*'&&flag) flag=0;
                poscol[j][i]=colnum;
            }
            if(flag==0) colnum++;
        }
       // colnum--;
     //   printf("row=%d,col=%d\n",rownum,colnum);
        memset(g,0,sizeof(g));
        uN=rownum;
        vN=colnum;
        for(int i=0;i<m;i++)
            for(int j=0;j<n;j++)
               if(str[i][j]=='*')
                g[posrow[i][j]][poscol[i][j]]=1;
        printf("%d\n",hungary());
    }
    return 0;
}


相關文章