zoj5093Battle ships【二分圖 棋盤覆蓋有斷點】
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.
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.
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;
}
相關文章
- 棋盤覆蓋
- 棋盤覆蓋問題
- 二分圖最小點覆蓋等於二分圖最大匹配
- 分治演算法-求解棋盤覆蓋問題演算法
- 棋盤完美覆蓋數(小規模原理實現)
- 二分圖最小點覆蓋構造方案+König定理證明
- Luogu P5089 元素週期表 / Codeforces 1012B Chemical table 題解 [ 並查集 ] [ 二分圖 ] [ 圖論建模 ] [ 棋盤覆蓋問題 ]並查集圖論
- 2023北航校賽-E 二分圖最小點覆蓋=n-最大獨立集
- 關於二分圖上的最大匹配、最小點覆蓋、最大獨立集以及最大權閉合子圖的聯絡
- 洛谷P7368 [USACO05NOV] Asteroids G 題解 二分圖最小點覆蓋 匈牙利演算法AST演算法
- 洛谷 P11011 點的覆蓋
- 最小路徑可重複點覆蓋
- bzoj1052: [HAOI2007]覆蓋問題(二分+搜尋)
- 最大匹配、最小頂點覆蓋、最大獨立集、最小路徑覆蓋(轉)(再轉)
- 矩形覆蓋
- [Python手撕]判斷二分圖Python
- 最小圓覆蓋
- Mysql索引覆蓋MySql索引
- 樹上最小點覆蓋的一類問題
- 3111. 覆蓋所有點的最少矩形數目
- 拆點判定二分圖
- 程式碼覆蓋率與測試覆蓋率比較
- 微信小程式背景圖完全覆蓋顯示微信小程式
- SOLIDWORKS 2023 工程圖的亮點新功能之一:材料明細表的覆蓋Solid
- 企業WiFi覆蓋,解決覆蓋四大難題WiFi
- 棋盤 K皇后
- 棋盤問題
- I. 棋盤
- 全球覆蓋 雜湊
- 線段覆蓋(挖
- 室內無線覆蓋設計有哪些選擇?
- idea2022.1 檢視單測覆蓋率展示分支覆蓋率Idea
- 景區無線覆蓋客流熱力圖解決方案圖解
- canvas 填充覆蓋描邊Canvas
- php實現矩形覆蓋PHP
- 【劍指Offer】矩形覆蓋
- ESLint: 規則配置覆蓋EsLint
- 線段覆蓋問題
- MySQL 索引覆蓋(Covering Index)MySql索引Index