zoj5093Battle ships【二分图 棋盘覆盖有断点】

来源:互联网 发布:西门子s7200软件下载 编辑:程序博客网 时间:2024/06/02 01:55

SubmitStatus

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
 

rt,去年暑假的时候做过HDU 1281 棋盘游戏 二分图最大匹配只允许每行每列放一个,只有一些点能放棋子,那么很容易就能想到以这个坐标行列连边。

这个题不是了,不仅这样,如果两个可放点中间有'#'那么这两个点也可放,求最多放多少“棋子”。

最开始想的是,遍历棋盘,每行、列可放的点最多个数作为与源点、汇点的连边流量,中间依旧是连横纵坐标。示例数据弱,没看出来问题,其实这么做不对。

正确解法:用‘#’分割的不同区域分成新的行、列,连边的序号是新的行、列号。二分图可搞,原理:对于分割开的区域,一个区域只能放一个棋子,那么就和原来的题一样了==

#include <iostream>#include<cstdio>#include<cstring>#include<map>using namespace std;#define M 550#define MAXN 50*50#define inf 0x3f3f3f3fint 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;}


0 0
原创粉丝点击