HDU 5093Battle ships
来源:互联网 发布:英雄无敌 mac 10.13 编辑:程序博客网 时间:2024/05/16 16:58
题意:有一片n*m的海,其中有冰山、浮冰和海水,现在向海里放战舰,战舰只能放在海水里,并且每一行每一列只能放一艘战舰,除非两艘战舰之间有冰山相隔,问最多能放多少艘战舰。
分析:很明显二分图匹配,把海水分成横着的块和竖着的块,每一块海水没有冰山相隔,所以一块只能放一艘战舰,当横着的块和竖着的块有相同的点时,就连接一条边,然后求最大匹配数目。
细节参见代码:
#include<iostream>#include<cstdio>#include<map>#include<vector>#include<string>#include<cstring>using namespace std;int t,n,m,cnt;char str[55][55];int x[55][55],y[55][55];vector<int> g[50*50+5];int from[50*50+5],tot;bool used[50*50+5];bool match(int x){ for(int i=0; i<g[x].size(); i++) if(!used[g[x][i]]) { used[g[x][i]]=true; if(from[g[x][i]]==-1||match(from[g[x][i]])) { from[g[x][i]]=x; return true; } } return false;}int hungary(){ tot=0; memset(from,-1,sizeof from); for(int i=1; i<=cnt; i++) { memset(used,0,sizeof used); if(match(i)) { ++tot; } }}void getx(){ int x1=1; for(int i=1; i<=n; i++) { for(int j=1; j<=m; j++) { if(str[i][j]=='*') x[i][j]=x1; if(str[i][j]=='#') x1++; } x1++; } cnt=x1;}int gety(){ int y1=1; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) { if(str[j][i]=='*') y[j][i]=y1; if(str[j][i]=='#') y1++; } y1++; } cnt=max(cnt,y1);}int main(){ scanf("%d",&t); while(t--) { cnt=0; memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); memset(str,0,sizeof(str)); scanf("%d%d",&n,&m); for(int i=1; i<=n; i++) scanf("%s",str[i]+1); getx(); gety(); for(int i=0;i<=n*m+1;i++) g[i].clear(); for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) if(str[i][j]=='*') g[x[i][j]].push_back(y[i][j]); hungary(); printf("%d\n",tot); } return 0;}
0 0
- hdu 5093 Battle ships
- HDU 5093 Battle ships
- HDU 5093Battle ships
- hdu 5093 Battle ships 二分图匹配
- hdu 5093 Battle ships(最大流)
- hdu 5093 Battle ships(二分图匹配)
- HDU 5093 Battle ships [二分图匹配]
- HDU 5093Battle ships(2014上海邀请赛)
- hdu 5093 Battle ships 二分匹配
- hdu 5093 Battle ships(二分匹配)
- hdu 5093 Battle ships 最大二分匹配
- HDU 5093 Battle ships 二分图!
- HDU 5093 Battle ships 二分图
- HDU 5093 Battle ships (最大匹配)
- HDU 5093 Battle ships(二分图)
- 【二分匹配】 HDU 5093 Battle ships 经典二分题
- HDU 5093 Battle ships(二分图+坐标变换)
- hdu 5093 Battle ships (二部图+最大匹配)
- arm qt自启+网络自动获取ip地址c
- 寄存器位写操作
- 为什么网页设计要使用栅格化
- 数据库权限设计
- 自定义Django Command命令
- HDU 5093Battle ships
- 源码-Oracle数据库管理-第十三章-子程序和包-Part 4(定义PL/SQL包)
- Zili's Backlog
- qq聊天记录词频查询 python实现
- 关于Tomcat启动报错:Failed to initialize end point associated with ProtocolHandler ["http-apr-8080"]
- Git 的 .gitignore 配置
- 十六进制转十进制
- 从头开始学java<五>
- Unix环境高级编程--多线程(一)