hdoj5093Battle ships【二分图最大匹配】
2016-01-08 13:56
316 查看
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1055 Accepted Submission(s): 373
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
2014上海全国邀请赛——题目重现(感谢上海大学提供题目)
#include<cstdio> #include<cstdlib> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int maxn=55; int n,m; int numy[maxn]; bool visy[maxn]; bool vis[maxn*maxn]; int p[maxn*maxn]; char map[maxn][maxn]; int G[maxn*maxn][maxn]; bool find(int k){ for(int i=0;G[k][i]!=-1;++i){ if(vis[G[k][i]])continue; vis[G[k][i]]=true; if(p[G[k][i]]==-1||find(p[G[k][i]])){ p[G[k][i]]=k; return true; } } return false; } int main() { int t,i,j,k; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(i=0;i<n;++i){ scanf("%s",map[i]); } for(i=0;i<m;++i){ numy[i]=i; } memset(G,-1,sizeof(G)); memset(p,-1,sizeof(p)); memset(visy,false,sizeof(visy)); int numx=0,num=0,cnt=0; for(i=0;i<n;++i){ for(j=0;j<m;++j){ if(map[i][j]=='*'){ visy[j]=true; G[numx][cnt++]=numy[j]; } else if(map[i][j]=='#'){ if(cnt!=0){ cnt=0; numx++; } if(visy[j]){ numy[j]=m+num;num++; visy[j]=false; } } } if(cnt){ cnt=0;numx++; } } int ans=0; for(i=0;i<numx;++i){ memset(vis,false,sizeof(vis)); if(find(i))ans++; } printf("%d\n",ans); } return 0; }
相关文章推荐
- ships 船 动态规划
- CoreData 从入门到精通(三)关联表的创建
- PHP任意文件上传漏洞(CVE-2015-2348)
- 【虚函数、虚拟指针、虚表指针】解析多重继承时,虚表的分布
- 【面试题】你是测试工程师,如何保证软件的质量?
- mongodb时间戳转换成格式化时间戳
- PHP一个表单多个提交按钮解决方法
- 利用pop完成类似倒计时效果
- 设置Launch Image 图片
- windows php7+nginx配置
- gridcontrol 添加行号
- C语言数据结构——单链表
- apktool,dex2jar,jd-gui简单使用与实战
- cocos2dx在win32中全屏
- Win10下文件资源管理器选中文件(夹)没有出现"选中状态"的终结解决方案
- linux常用小技巧一
- sql事务
- 《代码阅读方法和实践》二
- iOS视图层次结构
- google android volley framwork