Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1127 Accepted Submission(s): 411
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.
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
题意:
给你一个图,"*"代表海,"#"代表冰山,"o"代表浮冰。问你最多能停多少艘军舰在海上。但是有条件:任意两艘军舰不能在同一行或同一列,在同一列的唯一条件是中间有冰山隔开,浮冰不起隔作用,浮冰上面不停船。
思路:
这题主要在于思考怎样建图,建图的过程不好想。要将行和列分开。
*ooo 行集合(将 ’ * ‘标号) 1ooo 列集合 1ooo
o### o### o###
* *#* 22#3 23#4
ooo* ooo4 ooo4
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<queue>
using namespace std;
typedef long long ll;
const int N=555;
bool tu[N][N];
int from[N];///记录右边的点如果配对好了它来自哪里
bool use[N];///记录右边的点是否已经完成了配对
int n,m;///m,n分别表示两边的各自数量,n是左边,m是右边
bool dfs(int x)
{
for(int i=1; i<=m; i++) ///m是右边,所以这里上界是m
if(!use[i]&&tu[x][i])
{
use[i]=1;
if(from[i]==-1||dfs(from[i]))
{
from[i]=x;
return 1;
}
}
return 0;
}
int hungary()
{
int tot=0;
memset(from,-1,sizeof(from));
for(int i=1; i<=n; i++) ///n是左边,所以这里上界是n
{
memset(use,0,sizeof(use));
if(dfs(i))
tot++;
}
return tot;
}
char x[55][55];
int hang[55][55],lie[55][55];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(tu,0,sizeof(tu));
int M,N;
cin>>N>>M;
for(int i=1; i<=N; i++)
for(int j=1; j<=M; j++)
scanf(" %c",&x[i][j]);
int bj=1;
for(int i=1; i<=N; i++)
{
int flag=0;
for(int j=1; j<=M; j++)
{
if(x[i][j]=='*')
hang[i][j]=bj,flag=1;
else if(x[i][j]=='#'&&flag)
bj++,flag=0;
}
if(flag)
bj++;
}
n=bj;
bj=1;
for(int j=1; j<=M; j++)
{
int flag=0;
for(int i=1; i<=N; i++)
{
if(x[i][j]=='*')
lie[i][j]=bj,flag=1;
else if(x[i][j]=='#'&&flag)
bj++,flag=0;
}
if(flag)
bj++;
}
m=bj;
for(int j=1; j<=M; j++)
for(int i=1; i<=N; i++)
if(x[i][j]=='*')
tu[hang[i][j]][lie[i][j]]=1;
printf("%d\n",hungary());
}
return 0;
}