Battle ships
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 271 Accepted Submission(s): 128
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)都不能再用,就是一个二部图的思想。
参考代码:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int MAXN = 55*25;
char graph[MAXN][MAXN];
int nCase, m, n, idx, totalR, matching, pairRC[MAXN];
bool visitedC[MAXN];
vector<int> reachedC[MAXN];
struct {
int r, c;
} id[MAXN][MAXN];
void init() {
idx = matching = 0;
for (int i = 0; i < MAXN; i++) {
reachedC[i].clear();
}
memset(pairRC, -1, sizeof(pairRC));
}
void input() {
scanf("%d%d", &m, &n);
for (int i = 0; i < m; i++) {
scanf("%s", graph[i]);
}
}
bool findPath(int r) {
for (int i = 0; i < reachedC[r].size(); i++) {
int c = reachedC[r][i];
if (!visitedC[c]) {
visitedC[c] = true;
if (pairRC[c] == -1 || findPath(pairRC[c])) {
pairRC[c] = r;
return true;
}
}
}
return false;
}
void solve() {
for (int i = 0; i < m; i++) {
id[i][0].r = ++idx;
for (int j = 1; j < n; j++) {
if (graph[i][j] == '#' && graph[i][j] != graph[i][j-1]) {
id[i][j].r = ++idx;
} else {
id[i][j].r = idx;
}
}
if (graph[i][n-1] == '#') idx--;
}
totalR = idx;
for (int j = 0; j < n; j++) {
id[0][j].c = ++idx;
for (int i = 1; i < m; i++) {
if (graph[i][j] == '#' && graph[i][j] != graph[i-1][j]) {
id[i][j].c = ++idx;
} else {
id[i][j].c = idx;
}
}
if (graph[n-1][j] == '#') idx--;
}
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (graph[i][j] == '*') {
reachedC[id[i][j].r].push_back(id[i][j].c);
}
}
}
/*
for (int i = 1; i <= totalR; i++) {
printf("%d: ", i);
for (int j = 0; j < reachedC[i].size(); j++) {
printf("%d ", reachedC[i][j]);
}
printf("\n");
}
*/
for (int r = 1; r <= totalR; r++) {
memset(visitedC, false, sizeof(visitedC));
if (findPath(r)) {
matching++;
}
}
printf("%d\n", matching);
}
int main() {
scanf("%d", &nCase);
while (nCase--) {
init();
input();
solve();
}
return 0;
}