hdoj 5093 Battle ships 【二分图求最大匹配】【经典建图】
来源:互联网 发布:python代码大全 编辑:程序博客网 时间:2024/06/07 07:04
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 892 Accepted Submission(s): 313
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
24 4*oooo###**#*ooo*4 4#****#****#*ooo#
Sample Output
35
题意:有一个N*M的格子构成的矩阵,里面只有三种字符'o','#', '*'。‘o'表示该位置不能放置战舰,'*'表示该位置可以放置战舰。要求两艘战舰不能在同一行或者同一列碰面,除非中间有'#'把它们隔开,问你能够放置的最大战舰数。
分析:DFS必须超时,可以用匹配或者最大流来做。
思路:先扫描行,把连通的格子看成一个结点,这样可以得到二分图的X部,再同样扫描列,得到Y部。最后遍历原图,把有公共方格的结点连边建新图,然后对新图求最大匹配。
AC代码:
#include <cstdio>#include <cstring>#include <vector>#include <algorithm>#define MAXN 4000using namespace std;struct Node{ int x, y;};Node rec[60][60];vector<int> G[MAXN];char Map[60][60];bool used[MAXN];int pipei[MAXN];int row, cul;int N, M;void init(){ for(int i = 0; i <= MAXN; i++) G[i].clear();}void getMap()//建图{ row = 0, cul = 0; bool flag; flag = false;//判断 上一行最后碰到的是 * 还是 # //扫描行 合并同行中共存的点 for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { if(Map[i][j] == '*') rec[i][j].x = row, flag = true; if(Map[i][j] == '#') row++, flag = false; } if(flag)//最后出现的不是*则需要更新。 row++; } flag = false; //扫描列 合并同列中共存的点 for(int j = 0; j < M; j++) { for(int i = 0; i < N; i++) { if(Map[i][j] == '*') rec[i][j].y = cul, flag = true; if(Map[i][j] == '#') cul++, flag = false; } if(flag) cul++; } for(int i = 0; i < N; i++) { for(int j = 0; j < M; j++) { if(Map[i][j] == '*')//有公共格子的 连边 { int x = rec[i][j].x; int y = rec[i][j].y; G[x].push_back(y); } } }}int find(int x){ for(int i = 0; i < G[x].size(); i++) { int y = G[x][i]; if(!used[y]) { used[y] = true; if(pipei[y] == -1 || find(pipei[y])) { pipei[y] = x; return 1; } } } return 0;}void solve(){ int ans = 0; memset(pipei, -1, sizeof(pipei)); for(int i = 0; i < row; i++) { memset(used, false, sizeof(used)); ans += find(i); } printf("%d\n", ans);}int main(){ int t; scanf("%d", &t); while(t--) { scanf("%d%d", &N, &M); for(int i = 0; i < N; i++) scanf("%s", Map[i]); init(); getMap(); solve(); } return 0;}
0 0
- hdoj 5093 Battle ships 【二分图求最大匹配】【经典建图】
- hdoj 5093 Battle ships 【二分图最大匹配】
- HDOJ 5093 Battle ships 二分图匹配
- hdoj--5093--Battle ships(二分图经典建图)
- HDU 5093 -- Battle ships【二分图最大匹配 && 经典建图】
- HDU 5093 Battle ships(二分图最大匹配)
- hdu 5093 Battle ships 二分图匹配
- hdu 5093 Battle ships(二分图匹配)
- HDU 5093 Battle ships [二分图匹配]
- 【二分匹配】 HDOJ 5093 Battle ships
- HDOJ 5093 Battle ships 【二分匹配】
- hdu 5093 Battle ships 最大二分匹配
- hdu5093 Battle ships 二分图匹配
- hdu5093 Battle ships 二分图匹配
- HDOJ 题目5093 Battle ships(二分图)
- 【二分匹配】 HDU 5093 Battle ships 经典二分题
- HDU 5093 Battle ships(二分图匹配)
- HDU - 5093 Battle ships 初学 二分图匹配
- Spark GraphX Programming Guide 编程指南
- HDU 5387 Clock(模拟)——(多校练习8)
- Fragment进阶(四)----->参数传递3种写法
- AngularJS[ng-include]实现页面共用
- 应用内存onLowMemory& onTrimMemory优化
- hdoj 5093 Battle ships 【二分图求最大匹配】【经典建图】
- 设计模式:26 项目多也别傻做_享元模式
- android通知栏的实现
- 如何使用第三方字体
- 安卓基础
- ASP.net 分页插件
- org.apache.hadoop.hdfs.server.datanode.DataNode: Exception in BPOfferService for Block pool
- 获取公网IP
- 创建oracle数据库