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

随手一个匈牙利神奇AC,求解释

Posted by Mikucon at 2012-01-31 13:18:19 on Problem 3020
#include <iostream>
using namespace std;
int t,n,m;
bool g[42][42];
bool b[42][42];
int px[4]={0,0,1,-1};
int py[4]={1,-1,0,0};
int prex[42][42];
int prey[42][42];
bool find(int x,int y)
{
	for (int p=0;p<4;p++)
	{
		int xx=x+px[p],yy=y+py[p];
		if (g[xx][yy]&&!b[xx][yy])
		{
			b[xx][yy]=true;
			if (!prex[xx][yy]||find(prex[xx][yy],prey[xx][yy]))
			{
				prex[xx][yy]=x;
				prey[xx][yy]=y;
				return true;
			}
		}
	}
	return false;
}
int main()
{
	cin>>t;
	while (t--)
	{
		cin>>n>>m;
		for (int i=1;i<=n;i++)
			for (int j=1;j<=m;j++)
			{
				char c;
				cin>>c;
				g[i][j]=c=='*';
			}
		for (int i=1;i<=n;i++) g[i][0]=g[i][m+1]=false;
		for (int j=1;j<=m;j++) g[0][j]=g[n+1][j]=false;
		int count=0;
		memset(prex,0,sizeof(prex));
		memset(prey,0,sizeof(prey));
		int s=0;
		for (int i=1;i<=n;i++)
			for (int j=1;j<=m;j++)
				if (g[i][j])
				{
					s++;
					memset(b,0,sizeof(b));
					if (find(i,j)) count++;
				}
		cout<<(s*2-count)/2<<endl;//为何这样能算出答案?
	}
	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