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;
}
相關文章
- 分治演算法-求解棋盤覆蓋問題演算法
- 棋盤完美覆蓋數(小規模原理實現)
- 地圖自定義錨點+覆蓋物地圖
- 2023北航校賽-E 二分圖最小點覆蓋=n-最大獨立集
- 百度地圖 ~ 覆蓋物地圖
- 絕對定位的層判斷是否有相互覆蓋的解決演算法演算法
- 演算法題:頂點覆蓋問題演算法
- 【高德地圖API】從零開始學高德JS API(三)覆蓋物——標註|折線|多邊形|資訊視窗|聚合marker|麻點圖|圖片覆蓋物地圖APIJS
- Mysql索引覆蓋MySql索引
- iOS 防止輸入時鍵盤覆蓋掉輸入框iOS
- 程式碼覆蓋率與測試覆蓋率比較
- 演算法題系列:頂點覆蓋問題演算法
- 樹上最小點覆蓋的一類問題
- 企業WiFi覆蓋,解決覆蓋四大難題WiFi
- 第9條:覆蓋equals時總要覆蓋hashCode方法
- Java方法覆蓋和變數覆蓋的區別詳解Java變數
- EMMA 覆蓋率工具
- 語法:Python中的可覆蓋物件與不可覆蓋物件Python物件
- 使用swiper實現不完全覆蓋輪播圖
- 圖的遍歷演算法-馬遍歷棋盤演算法
- 專案總結二:解決鍵盤覆蓋tableview的問題View
- canvas 填充覆蓋描邊Canvas
- pHp程式碼覆蓋率PHP
- php實現矩形覆蓋PHP
- 程式碼覆蓋率分析
- 景區無線覆蓋客流熱力圖解決方案圖解
- 百度筆試題:繩子最多覆蓋多少個點筆試
- 百萬級GMV視訊號覆盤3點思考
- 四、建立覆蓋網路--Flannel
- 76. 最小覆蓋子串
- 馬踏棋盤(棧實現)
- 綜合體無線網路覆蓋的解決方案有什麼?
- 都100%程式碼覆蓋了,還會有什麼問題?
- SOLIDWORKS 2023 工程圖的亮點新功能之一:材料明細表的覆蓋Solid
- hdu4751Divide Groups【判斷二分圖】IDE
- 4 20覆盤
- 百度地圖javascript開發,刪除指定覆蓋物方法地圖JavaScript
- 222.map4-百度地圖新增覆蓋物地圖