Bestcoder round #1 hdu4859 (coastline)

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 4859

 

Coastline

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 137 accepted submission (s): 76


Problem description welcome to Zhuhai!

Due to the increasing shortage of land resources, many coastal cities can only rely on reclamation to expand their urban areas for development. As the decision maker of the city Z, after carefully observing the map of the city Z, you are going to expand the coastline of the city Z to the longest by filling in some sea areas to attract more tourists to travel and vacation. In order to simplify the problem, assume that the map is a grid of N * m, some of which are land, some are shallow waters that can be filled, and some are infilled deep waters. The length of the coastline is defined as the edge length of a connecting block of land (which may contain the land filled in the shallow sea area). The two grids have at least one public edge, which is considered as China Unicom.

It is worth noting that the land areas of the city Z can be different, and the whole map is in the ocean. That is to say, the city Z is composed of islands, such as images, hawaii?

Your task is to fill in some shallow waters to make the coastline of all islands the longest.

 

Input the first line T, indicating that there are T groups of test data.
Each group of data starts with two integers n and M, indicating the map size. In the next n rows, each row contains a string of M, indicating a map. '.' indicates land, 'E' indicates shallow sea, and 'D' indicates deep sea.

[Technical Specification]

1. 1 <= T <= 100
2. 1 <= n, m <= 47

 

For each group of data, the output is the first group of data, and then the maximum length of the coastline is output.

 

Sample input32 2eeee3 3eee. E. eee3 3 eeededeee

 

Sample outputcase 1: 8 Case 2: 16 Case 3: 20 HintFor the third group of samples, a feasible solution is:. E. D. E. The total length of the five isolated islands is 4*5 = 20.

 

Author [email protected]

 

Sourcebestcoder round #1... the code of http://www.kuangbin.net/archives/hdu4859is very good. This question is very well solved. Doesn't
#include <iostream>#include <cstring>using namespace std;#define rep(i, n) for (int i = 0, _n = (int)(n); i < _n; i++)#define fer(i, x, n) for (int i = (int)(x), _n = (int)(n); i < _n; i++)#define rof(i, n, x) for (int i = (int)(n), _x = (int)(x); i-- > _x; )#define fch(i, x) for (__typeof(x.begin()) i = x.begin(); i != x.end(); i++)#define sz(x) (int((x).size()))#define pb push_back#define mkp make_pair#define all(X) (X).begin(),(X).end()#define X first#define Y secondtemplate<class T> inline void smin(T &a, T b){if(b<a)a=b;}template<class T> inline void smax(T &a, T b){if(a<b)a=b;}template<class T> inline void RST(T &A){memset(A, 0, sizeof(A));}template<class T> inline void FLC(T &A, int x){memset(A, x, sizeof(A));}template<class T> inline void CLR(T &A){A.clear();}/** Constant List .. **/ //{const int dx4[] = {-1, 0, 1, 0};const int dy4[] = {0, 1, 0, -1};const int dx8[] = {-1, 0, 1, 0 , -1 , -1 , 1 , 1};const int dy8[] = {0, 1, 0, -1 , -1 , 1 , -1 , 1};const int mod = 1000000007;const int INF = 0x3f3f3f3f;//}template<class T> inline T min(T a, T b, T c){return min(min(a, b), c);}template<class T> inline T max(T a, T b, T c){return max(max(a, b), c);}template<class T> inline T min(T a, T b, T c, T d){return min(min(a, b), min(c, d));}template<class T> inline T max(T a, T b, T c, T d){return max(max(a, b), max(c, d));}template<class T> inline T sqr(T a){return a*a;}template<class T> inline T cub(T a){return a*a*a;}////////////////////////////////////////////////////////////////////////////////const int MAXN = 10010;const int MAXM = 100010;struct Edge{    int to,next,cap,flow;}edge[MAXM];int tol;int head[MAXN];int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];void init(){    tol = 0;    memset(head,-1,sizeof(head));}void addedge(int u,int v,int w,int rw = 0){    edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];    edge[tol].flow = 0; head[u] = tol++;    edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];    edge[tol].flow = 0;head[v] = tol++;}int sap(int start,int end,int N){    memset(gap,0,sizeof(gap));    memset(dep,0,sizeof(dep));    memcpy(cur,head,sizeof(head));    int u = start;    pre[u] = -1;    gap[0] = N;    int ans = 0;    while(dep[start] < N)    {        if(u == end)        {            int Min = INF;            for(int i = pre[u];i != -1;i = pre[edge[i^1].to])                if(Min > edge[i].cap - edge[i].flow)                    Min = edge[i].cap - edge[i].flow;            for(int i = pre[u];i != -1;i = pre[edge[i^1].to])            {                edge[i].flow += Min;                edge[i^1].flow -= Min;            }            u = start;            ans += Min;            continue;        }        bool flag = false;        int v;        for(int i = cur[u];i != -1;i = edge[i].next)        {            v = edge[i].to;            if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])            {                flag = true;                cur[u] = pre[v] = i;                break;            }        }        if(flag)        {            u = v;            continue;        }        int Min = N;        for(int i = head[u];i != -1;i = edge[i].next)            if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)            {                Min = dep[edge[i].to];                cur[u] = i;            }        gap[dep[u]]--;        if(!gap[dep[u]])return ans;        dep[u] = Min+1;        gap[dep[u]]++;        if(u != start)u = edge[pre[u]^1].to;    }    return ans;}int T,n,m;char s[50][50];int id[50][50];int start,end;int main(){    //freopen("in.txt","r",stdin);    ios_base::sync_with_stdio(0);    scanf("%d",&T);    fer(tt,1,T+1){        scanf("%d%d",&n,&m);        fer(i,1,n+1) scanf("\n%s",s[i]+1);        rep(i,n+2) s[i][0]=s[i][m+1]=‘D‘;        rep(i,m+2) s[0][i]=s[n+1][i]=‘D‘;        init();        int cnt=0;        rep(i,n+2) rep(j,m+2) id[i][j]=++cnt;        start=0,end=cnt+1;        int sume = 0;        rep(i,n+2) rep(j,m+2){            rep(k,4){                int tx=i+dx4[k],ty=j+dy4[k];                if(tx<0 || tx>=n+2 || ty<0 || ty>=m+2) continue;                sume++; addedge(id[i][j],id[tx][ty],1);            }            if(s[i][j]!=‘E‘){                if( ((i+j)%2==1 && s[i][j]==‘.‘) || ((i+j)%2==0 && s[i][j]==‘D‘) ) addedge(start,id[i][j],INF);                else addedge(id[i][j],end,INF);            }        }        printf("Case %d: %d\n",tt,sume/2-sap(start,end,cnt+2) );    }    return 0;}

 

Bestcoder round #1 hdu4859 (coastline)

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.