Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 893 Accepted Submission(s): 314
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
Source
题意问的就是在保证炮台之间不相互残杀的条件,图中最多能放几个炮台,炮台
放的位置的那一横行和一竖行都是炮台得火力范围,除非炮台中间有冰山也就是#号,o表示
浮冰,*表示炮台可以建的位置。
这题和1045有点像,只不过这道题多了一个浮冰的条件,浮冰上不能建炮台,但是炮台的子弹
可以穿过浮冰。用dfs果断超时,,然后就用了缩点二分图匹配过了,解题报告和1045差不太多就不多赘述了。
#include<stdio.h>
#include<string.h>
#define M 1000
char mp[M][M];
int r[M][M],c[M][M],ruse[M],cuse[M],vis[M],path[M][M];
int n,m,cmax,rmax;
int find(int s){
for(int i=1;i<cmax;i++){
if(!vis[i] && path[s][i]){
vis[i]=1;
if(!cuse[i] || find(cuse[i])){
cuse[i]=s;
ruse[s]=i;
return 1;
}
}
}
return 0;
}
int main(){
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=1;i<=n;i++){
getchar();
for(j=1;j<=n;j++){
scanf("%c",&mp[i][j]);
}
}
memset(r,-1,sizeof(r));
memset(c,-1,sizeof(c));
rmax=cmax=1;
for(i=1;i<=n;i++){
for(j=1;j<=m;j++){
if(mp[i][j]=='*'&&r[i][j]==-1){//横向缩点
for(int k=j; (mp[i][k]=='*' || mp[i][k]=='o')&&k<=m ;k++)
r[i][k]=rmax;
rmax++;
}
if(mp[j][i]=='*'&&c[j][i]==-1){//纵向缩点
for(int k=j;(mp[k][i]=='*' || mp[k][i]=='o')&&k<=n ;k++)
c[k][i]=cmax;
cmax++;
}
}
}
memset(path,0,sizeof(path));
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
if(mp[i][j]=='*')
path[ r[i][j] ][ c[i][j] ]=1;//建图
}
int ans=0;
memset(ruse,0,sizeof(ruse));
memset(cuse,0,sizeof(cuse));
for(i=1;i<rmax;i++){
memset(vis,0,sizeof(vis));
ans+=find(i);
}
printf("%d\n",ans);
}
return 0;
}