Battle ships
Time limit:2000/1000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 553 Accepted Submission (s): 223
Problem Descriptiondear contestant, now a excellent Navy commander, who is responsible of a tough mission Ly.
Your Fleet Unfortunately encountered an enemy fleet near the South Pole where the geographical conditions is negative for Both sides. The floating ice and iceberg blocks battleships move which leads to this unexpected engagement highly dangerous, unpredict Able and incontrollable.
But, fortunately, as a experienced navy commander, you is able to take opportunity-embattle the ships to maximize the Utility of cannons on the battleships before the engagement.
The target is a arrange as many battleships as you can in the map. However, there is three rules so which you cannot does that arbitrary:
A battleship cannot lay on floating ice
A battleship cannot be placed in an iceberg
The battleships cannot is arranged in the same row or column, and unless one or more icebergs is in the middle of them.
Inputthere is only one of the integer T (0<t<12) at the beginning line, which means following T test cases.
For each test case, integers m and n (1 <= m, n <=) is 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 the ' # ', ' * ', ' o ', that symbolize iceber G, ordinary sea and floating ice.
Outputfor each case, output just one line, contains a single integer which represents the maximal possible number of Battl Eships can be arranged.
Sample Input24 4*oooo###**#*ooo*4 4#****#****#*ooo#
Sample Output35
The key is to build the map
Horizontal and vertical sub-blocks are respectively ++tot, connected, and then directly Hungarian algorithm
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <cmath > #include <algorithm> #include <cstdlib>using namespace Std;int n,m,tot;char s[51][51];int dx[51][51], Dy[51][51],xxn,mp[2510][2510],yyn,link[2510],mark[2510];bool dfs (int x) {for (int i=1;i<=yyn;i++) {if (mp[x ][i]&&mark[i]==-1) {mark[i]=1; if (link[i]==-1| | DFS (Link[i])) {link[i]=x; return true; }}} return false;} int main () {int TT; scanf ("%d", &TT); while (tt--) {scanf ("%d%d", &n,&m); BOOL Flag; tot=0; Memset (Mp,0,sizeof (MP)); memset (link,-1,sizeof (link)); memset (dx,0,sizeof (DX)); memset (dy,0,sizeof (dy)); for (int i=0;i<n;i++) scanf ("%s", S[i]); for (int i=0;i<n;i++) {flag=false; for (int j=0;j<m;j++) { if (s[i][j]== ' * ') {if (!flag) dx[i][j]=++tot,flag=true; else Dx[i][j]=tot; } else if (s[i][j]== ' # ') Flag=false; }} Xxn=tot; tot=0; for (int j=0;j<m;j++) {flag=false; for (int i=0;i<n;i++) {if (s[i][j]== ' * ') {if (!flag) {Dy[i][j]=++tot; Flag=true; } else Dy[i][j]=tot; } else if (s[i][j]== ' # ') Flag=false; }} Yyn=tot; for (int i=0;i<n;i++) {for (int j=0;j<m;j++) {int xx,yy; XX=DX[I][J],YY=DY[I][J]; if (XX&&YY) Mp[xx][yy]=1; }} int ans=0; for (int i=1;i<=xxn;i++) {memset (mark,-1,sizeof (Mark)); if (Dfs (i)) ans++; } printf ("%d\n", ans); } return 0;}
(Hungarian algorithm) Hdu 5093