poj2446 chessboard

明明说了m,n不超过32,死活过不去,最后设置成2000才过,测试范围难道就是随便说说的?

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<map> 
const int maxn = 1e3+10;
using namespace std;
map<char,int> mp; 
int tot,head[maxn],ans,flag[maxn][maxn],n,m,k;
struct Edge{
	int to,nxt;
}e[maxn*maxn];
int vis[maxn],link[maxn];
void add(int from,int to)
{
	//cout << from << " " << to <<"\n";
	e[tot].nxt = head[from], e[tot].to = to;
	head[from] = tot++;
}
bool dfs(int fa)
{
	for(int i = head[fa]; ~i; i = e[i].nxt)
	{
		if(!vis[e[i].to])
		{
			vis[e[i].to] = 1; 
			if(!link[e[i].to] || dfs(link[e[i].to]))
			{
				link[e[i].to] = fa;
				//cout << fa << " " << e[i].to << "\n";
				return true;
			}
		} 
	}
	return false;
}
int _hash(int i,int j)
{
	return (i-1)*m+j;
}
char op[maxn][5];
int main()
{
	
	while(~scanf("%d%d%d",&n,&m,&k))
	{
		memset(flag,0,sizeof(flag));
		memset(head,-1,sizeof(head)); tot = 0;
		memset(link,0,sizeof(link)); ans = 0;
		for(int i = 0,a,b; i < k; ++i) scanf("%d%d",&a,&b),flag[b][a]=1;
		for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
		{
			if(flag[i][j]) continue;
			if(j > 1 && !flag[i][j-1]) add(_hash(i,j),_hash(i,j-1));
			if(j < m && !flag[i][j+1]) add(_hash(i,j),_hash(i,j+1));
			if(i > 1 && !flag[i-1][j]) add(_hash(i,j),_hash(i-1,j));
			if(i < n && !flag[i+1][j]) add(_hash(i,j),_hash(i+1,j));
		}
		for(int i = 1; i <= n; ++i)
		for(int j = 1; j <= m; ++j)
		{
			if(flag[i][j]) continue;
			memset(vis,0,sizeof(vis));
			if(dfs(_hash(i,j))) ans++;
		}
		//cout << ans << " " << n*m << "\n";
		if(ans + k  == n*m) printf("YES\n");
		else printf("NO\n");
	}
}

 

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务