HDOJ 5093 Battle ships 二分图匹配

简介:

二分图匹配:

分别按行和列把图展开。hungary二分图匹配。

。。。

例子:
4 4
*ooo
o###
**#*
ooo*
按行展开。

。。

。 *ooo o#oo oo#o ooo# **#o ooo* ooo* 再按列展开。。。

7 * 8 *ooooooo oooooooo oooooooo oooooooo *o*ooooo ooooooo* ooooooo* 匹配结果3



Battle ships

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


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
 
 
2 4 4 *ooo o### **#* ooo* 4 4 #*** *#** **#* ooo#
 

Sample Output
 
 
3 5
 

Source
 



#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int maxn=2600;

char mp[55][55];
char hang[maxn][maxn];
char lie[maxn][maxn];

int n,m;
int nn,mm;

void getHang()
{
  nn=0;
  for(int i=0;i<n;i++)
    {
      for(int j=0;j<m;j++)
        {
          if(mp[i][j]=='o')
            {
              hang[nn][j]='o';
            }
          else if(mp[i][j]=='*')
            {
              hang[nn][j]='*';
            }
          else if(mp[i][j]=='#')
            {
              hang[nn][j]='#';
              for(int k=j+1;k<m;k++)
                {
                  hang[nn][k]='o';
                }
              if(j!=m-1)
                {
                  nn++;
                  for(int k=0;k<=j;k++)
                    {
                      hang[nn][k]='o';
                    }
                }
            }
        }
      nn++;
    }
  /*********************debug***********************
  cout<<" debug hang \n";
  for(int i=0;i<nn;i++)
    {
      printf("%s\n",hang[i]);
    }
  *********************debug***********************/
}

void getLie()
{
  mm=0;
  for(int i=0;i<m;i++)
    {
      for(int j=0;j<nn;j++)
        {
          if(hang[j][i]=='*')
            {
              lie[j][mm]='*';
            }
          else if(hang[j][i]=='o')
            {
              lie[j][mm]='o';
            }
          else if(hang[j][i]=='#')
            {
              for(int k=j;k<nn;k++)
                {
                  lie[k][mm]='o';
                }
              if(j!=nn-1)
                {
                  mm++;
                  for(int k=0;k<=j;k++)
                    {
                      lie[k][mm]='o';
                    }
                }
            }
        }
      mm++;
    }
  /**************debug lie*******************
  cout<<"debug lie\n";
  cout<<nn<<" * "<<mm<<endl;
  for(int i=0;i<nn;i++)
    {
      for(int j=0;j<mm;j++)
        {
          printf("%c",lie[i][j]);
          if(j==mm-1) putchar(10);
        }
    }
  **************debug lie*******************/
}

struct Edge
{
  int to,next;
}edge[maxn*maxn];

int Adj[maxn],Size;

void init()
{
  memset(Adj,-1,sizeof(Adj)); Size=0;
}

void add_edge(int u,int v)
{
  edge[Size].to=v; edge[Size].next=Adj[u]; Adj[u]=Size++;
}

int linker[maxn];
bool used[maxn];

bool dfs(int u)
{
  for(int i=Adj[u];~i;i=edge[i].next)
    {
      int v=edge[i].to;
      if(!used[v])
        {
          used[v]=true;
          if(linker[v]==-1||dfs(linker[v]))
            {
              linker[v]=u;
              return true;
            }
        }
    }
  return false;
}

int hungary()
{
  int res=0;
  memset(linker,-1,sizeof(linker));
  for(int u=0;u<nn;u++)
    {
      memset(used,false,sizeof(used));
      if(dfs(u)) res++;
    }
  return res;
}

int main()
{
  int T_T;
  scanf("%d",&T_T);
  while(T_T--)
    {
      memset(mp,0,sizeof(mp));
      memset(hang,0,sizeof(hang));
      memset(lie,0,sizeof(lie));
      init();

      scanf("%d%d",&n,&m);
      for(int i=0;i<n;i++)
          scanf("%s",mp[i]);

      getHang();
      getLie();

      /// build graph

      for(int i=0;i<nn;i++)
        {
          for(int j=0;j<mm;j++)
            {
              if(lie[i][j]=='*')
                {
                  add_edge(i,j);
                }
            }
        }

      printf("%d\n",hungary());
    }
  return 0;
}








本文转自mfrbuaa博客园博客,原文链接:http://www.cnblogs.com/mfrbuaa/p/5262022.html,如需转载请自行联系原作者
相关文章
|
定位技术 API 网络架构
地图图层接入:从mapbox转向cesium
由于地图坐标系的不统一,地图图商提供的图层服务也各有特色,在图层对接的开发过程中常会遇到许多坑,从二维图层到三维图层,地图引擎mapbox再到cesium,本文将分享笔者在近期地图图层接入过程中总结的一些经验。
4551 10
|
机器学习/深度学习 编解码 算法
【YOLO系列】YOLOv1论文超详细解读(翻译 +学习笔记)
【YOLO系列】YOLOv1论文超详细解读(翻译 +学习笔记)
2212 0
【YOLO系列】YOLOv1论文超详细解读(翻译 +学习笔记)
【光波电子学】MATLAB绘制光纤中线性偏振模式LP之单模光纤的电场分布(光斑)
该文章介绍了如何使用MATLAB绘制单模光纤中线性偏振模式LP₀₁的电场分布,并提供了相关的数学公式和参数用于模拟光纤中的光斑分布。
289 0
|
API 定位技术 开发工具
Android Studio2021.1.1 高德地图api调用这一篇就够了
Android Studio2021.1.1 高德地图api调用这一篇就够了
|
Unix
全网首发:configure: error: cannot guess build type; you must specify one
全网首发:configure: error: cannot guess build type; you must specify one
997 0
|
NoSQL Linux Redis
centos7 systemctl 启动 Redis 失败
转自:http://sloger.info/posts/systemd-failed-to-start-redis-in-gentoo 今天启动 Redis 时阻塞很长时间,之后显示启动失败,启动状态如下。
6064 0
|
缓存 Ubuntu 编译器
CMake 常见问题及解决办法
CMake 常见问题及解决办法
5667 0
Zp
|
SQL Oracle 关系型数据库
关于操作数据库时报的java.sql.SQLException: ORA-00904:标识符无效 的问题
关于操作数据库时报的java.sql.SQLException: ORA-00904:标识符无效 的问题
Zp
647 0