Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

我又来无耻地贴代码了,32MS

Posted by speedcell4 at 2011-12-01 13:41:01 on Problem 3020
#include<iostream>

using namespace std;

#define within(x,y) ((x)>=0&&(y)>=0&&(x)<n&&(y<m))

const int MAXN = 50;
const int go[4][2]={{1,0},{-1,0},{0,1},{0,-1}};

char G1[MAXN][MAXN];
bool G2[MAXN*MAXN][MAXN*MAXN];
int tcase,n,m;

int M[MAXN*MAXN];
bool vis[MAXN*MAXN];

bool Augment_Path(int u)
{
    int i,l;
    for(i=0,l=n*m;l-i>0;i++)
    {
        if(u!=i&&G2[u][i]&&!vis[i])
        {
            vis[i]=true;
            if(M[i]==-1||Augment_Path(M[i]))
            {
                M[i]=u;
                M[u]=i;
                return true;
            }
        }
    }
    return false;
}
int Hungary(void)
{
    int i,l,cnt=0;
    memset(M,-1,sizeof(M));
    
    for(i=0,l=n*m;l-i>0;i++)
    {
        if(M[i]==-1)
        {
            memset(vis,false,sizeof(vis));
            cnt+=Augment_Path(i);
        }
    }
    
    return cnt;
}
int main()
{
    int cnt,i,j,k,xx,yy;
    scanf("%d",&tcase);while(tcase--)
    {
        cnt=0;
        scanf("%d %d",&n,&m);
        for(i=0;n-i>0;i++)
        {
            getchar();
            for(j=0;m-j>0;j++)
            {
                scanf("%c",&G1[i][j]);
                if(G1[i][j]=='*') cnt++;
            }
        }
        memset(G2,false,sizeof(G2));
        for(i=0;n-i>0;i++)
        {
            for(j=0;m-j>0;j++)
            {
                for(k=0;4-k>0;k++)
                {
                    xx=i+go[k][0];
                    yy=j+go[k][1];
                    if(G1[i][j]=='*'&&within(xx,yy)&&G1[xx][yy]=='*') G2[i*m+j][xx*m+yy]=G2[xx*m+yy][i*m+j]=true;
                }
            }
        }
        printf("%d\n",cnt-Hungary());
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator