HDU 5093 -- Battle ships【二分图最大匹配 && 经典建图】
来源:互联网 发布:acdsee激活软件 编辑:程序博客网 时间:2024/06/05 00:35
Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 898 Accepted Submission(s): 315
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代表冰水,#代表冰山,要想在海域中放置船,保证船与船之间不能相互看到,之间只要有山就不能看到,问最多能放多少船。
将一片最多只能放一个船的连续网格叫做‘块’。
以样例一为例
首先只考虑行,将每个块标号:将答案存入x [ ] [ ]
1000
0000
2203
0004
再此只考虑列,将每个块标号:将答案存入y [ ][ ]
1000
0000
2304
0004
void getmap(){ for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j) if(str[i][j] == '*') map[x[i][j]][y[i][j]] = 1; } return ;}
上面代码相当于坐标(i, j)所在的行块和列块实现了一一映射关系。
所以将行块看做一个集合,列块看做一个集合。
所求的最大放船数就是两个集合之间的最大匹配数。
#include <cstdio>#include <cstring>#include <algorithm>#define maxn 55using namespace std;int map[maxn * 55][maxn * 55];char str[maxn][maxn];int used[maxn * 55];int link[maxn * 55];int x[maxn][maxn];int y[maxn][maxn];int n, m;int x1, y1;void init(){ memset(map, 0, sizeof(map)); memset(x, 0, sizeof(x)); memset(y, 0, sizeof(y));}void input(){ scanf("%d%d", &n, &m); for(int i = 0; i < n; ++i) scanf("%s", str[i]);}void creat_x(){ x1 = 1; for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j){ if(str[i][j] == '*') x[i][j] = x1; if(str[i][j] == '#') x1++; } x1++; } return ;}void creat_y(){ y1 = 1; for(int j = 0; j < m; ++j){ for(int i = 0; i < n; ++i){ if(str[i][j] == '*') y[i][j] = y1; if(str[i][j] == '#') y1++; } y1++; } return ;}void getmap(){ for(int i = 0; i < n; ++i){ for(int j = 0; j < m; ++j) if(str[i][j] == '*') map[x[i][j]][y[i][j]] = 1; } return ;}bool dfs(int x){ for(int i = 1; i < y1; ++i){ if(map[x][i] && !used[i]){ used[i] = 1; if(link[i] == -1 || dfs(link[i])){ link[i] = x; return true; } } } return false;}int hungary(){ int ans = 0; memset(link, -1, sizeof(link)); for(int j = 1; j < x1; ++j){ memset(used, 0, sizeof(used)); if(dfs(j)) ans++; } return ans;}int main (){ int T; scanf("%d", &T); while(T--){ init(); input(); creat_x();// for(int i = 0; i < n; ++i){// for(int j = 0; j < m; ++j)// printf("%d", x[i][j]);// printf("\n");// } creat_y();// for(int i = 0; i < n; ++i){// for(int j = 0; j < m; ++j)// printf("%d", y[i][j]);// printf("\n");// } //printf("--%d %d\n", x1,y1); getmap(); int sum = hungary(); printf("%d\n", sum); } return 0;}
1 0
- HDU 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 经典二分题
- 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(最大流 || 二分图)
- UIApplicationMain
- hdu 1316
- hadoop about "Container does not exist."
- AndroidStudio 代码混淆proguard记录
- nodejs co generator 调用另外一个generator
- HDU 5093 -- Battle ships【二分图最大匹配 && 经典建图】
- iOS —— NSRunLoop / NSPart
- oracle 10g的备份和还原
- lytro原始图片重聚焦
- C和C++格式化输入输出
- Linux基本命令一
- 黑马程序员--Java集合框架(2)
- hadoop2.6.0编译使用
- oracle rac service学习