HDU 5093 Battle ships (maximum matching of two images)
Battle shipsTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 271 Accepted Submission (s): 128
Problem DescriptionDear 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 your 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.
InputThere is only one integer T (0
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.
OutputFor 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
Source2014 Shanghai national invitational competition-reproduction of the questions (thanks to Shanghai University for providing the questions)
When a warship is placed at sea (*), no two warships can appear in the same row or column unless there is an iceberg (#) in the middle. Ask how many warships are allowed at most.
Solution: I didn't want to use a two-part diagram to solve the problem during the competition. Later I found that it was a classic question type in the diagram of the second part of the white book. The rows and columns are separated by iceberg (#) and separated into multiple segments. For each possible position (*), the row and column segment are connected. The expression means that if this point is placed, the adjacent same segment (*, o) cannot be used any more. It is a two-part diagram.
Reference code:
#include
#include
#include
#include
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
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;}