HDU5093 Battle Ships (two-part picture)

Source: Internet
Author: User

Test instructions: give you a m*n (1<=m,n<=50) figure, where ' # ' represents the iceberg, ' * ' represents the ocean, ' o ' represents the ice floe. Then let you place as many boats as possible, but to satisfy the rules:

    1. A ship cannot be placed on an iceberg;
    2. The ship cannot be placed on the ice.
    3. The two ships cannot be in the same column or row unless there is an iceberg between them.

Analysis: The two best match of red fruits .... The diagram is also very easy to construct ... The Ocean lattice connected block (at least one lattice) separated by an iceberg is used as the X-point, and each column of the same row is separated by an iceberg as a Y-point, and the X-point is connected to the Y-point with an edge, and only if the two connected blocks share a single ocean lattice. Then run the two-point maximum match ... 2,500 points, duly completed.

As if it is the official work, but can not, the next array to Nextt to be able to cross C + +, g++.

#include <stdlib.h> #include <cmath> #include <cstring> #include <iostream> #include <map > #include <set> #include <algorithm> #include <queue> #include <vector> #include <cstdio >using namespace std; #define N 205#define M 50005int ev[m],nextt[m];int head[m];int cnt;bool vis[m];int Pre[m];char S[N    ][n];int x[n][n],y[n][n];int pn[2];void addedge (int u,int v) {ev[cnt]=v;    Nextt[cnt]=head[u];    head[u]=cnt++; return;}        int find (int x) {for (int i=head[x];~i;i=nextt[i]) {int v=ev[i];            if (!vis[v]) {vis[v]=1; if (pre[v]==-1| |                Find (Pre[v])) {pre[v]=x;            return 1; }}} return 0;}    int main () {int t;    scanf ("%d", &t);        while (t--) {int n,m;        memset (head,-1,sizeof (head));        cnt=0;        memset (pre,-1,sizeof (pre));        memset (x,0,sizeof (x));        memset (y,0,sizeof (y));        scanf ("%d%d", &n,&m); for (int i=0;i<n;i++) scanf ("%s", S[i]);        int num=0;            for (int i=0;i<n;i++) {bool flag=0;                    for (int j=0;j<m;j++) {if (s[i][j]== ' * ') {if (flag==0) num++;                    X[i][j]=num;                flag=1;            } else if (s[i][j]== ' # ') flag=0;        }} Pn[0]=num;        num=0;            for (int i=0;i<m;i++) {bool flag=0;                    for (int j=0;j<n;j++) {if (s[j][i]== ' * ') {if (flag==0) num++;                    Y[j][i]=num;                flag=1;            } else if (s[j][i]== ' # ') flag=0;        }} Pn[1]=num;            for (int i=0;i<n;i++) for (int j=0;j<m;j++) {int u = x[i][j],v=y[i][j];        if (u&&v) Addedge (U,V);        } int ans=0; for (int i=1;i<=pn[0];++i) {memset (vis,0,sizeof (VIS));        Ans+=find (i);    } printf ("%d\n", ans); } return 0;}


HDU5093 Battle Ships (two-part picture)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.