bzoj 1443: [JSOI2009]游戏Game

Description

hh

Solution

和兔兔与蛋蛋类似,只需要判断该点是否是先手必胜就行
没有想出正解,写了个暴力剪枝过去了
正解是:输出最大匹配中的非必需点
我的做法是暴力枚举起始位置,匹配数组不清空,复杂度 \(O(n^4)\)

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <cmath>
#define RG register
#define il inline
#define iter iterator
#define Max(a,b) ((a)>(b)?(a):(b))
#define Min(a,b) ((a)<(b)?(a):(b))
using namespace std;
typedef long long ll;
const int N=105,M=10005;
int n,m;char a[N][N];
int head[M],nxt[M*10],to[M*10],num=0,id[N][N],cnt=0,NOW=0;
void add(int x,int y){nxt[++num]=head[x];to[num]=y;head[x]=num;}
inline void link(int x,int y){add(x,y);add(y,x);}
void build(){
	for(RG int i=1;i<=n;i++)
		for(RG int j=1;j<=m;j++)
			if(a[i][j]=='.')id[i][j]=++cnt;
	for(RG int i=1;i<=n;i++)
		for(RG int j=1;j<=m;j++){
			if(a[i][j]=='#')continue;
			if(a[i][j+1]=='.')link(id[i][j],id[i][j+1]);
		   if(a[i+1][j]=='.')link(id[i][j],id[i+1][j]);
		}
}
bool vis[M];int b[M],st[M],top=0;
inline bool dfs(int x){
	for(RG int i=head[x];i;i=nxt[i]){
		int u=to[i];
		if(!vis[u] && u!=NOW){
			vis[u]=1;st[++top]=u;
			if(!b[u] || dfs(b[u])){
				b[u]=x;b[x]=u;
				return true;
			}
		}
	}
	return false;
}
int sum=0,ax[M],ay[M];
inline void solve(int x,int y){
	if(!b[id[x][y]])ax[++sum]=x,ay[sum]=y;
	else{
		int u=id[x][y],v=b[u];
		b[v]=b[u]=0;NOW=u;
		memset(vis,0,sizeof(vis));
		if(dfs(v))ax[++sum]=x,ay[sum]=y;
		else{
			NOW=0;memset(vis,0,sizeof(vis));dfs(u);
		}
	}
}
void work()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
		scanf("%s",a[i]+1);
	build();
	for(int i=1;i<=cnt;i++)
		if(!b[i]){
			memset(vis,0,sizeof(vis));
			dfs(i);
		}
	for(RG int i=1;i<=n;i++)
		for(RG int j=1;j<=m;j++)
			if(a[i][j]=='.')solve(i,j);
	if(!sum)puts("LOSE");
	else{
		puts("WIN");
		for(int i=1;i<=sum;i++)printf("%d %d\n",ax[i],ay[i]);
	}
}

int main()
{
	freopen("pp.in","r",stdin);
	freopen("pp.out","w",stdout);
	work();
	return 0;
}

posted @ 2017-12-30 15:45  PIPIBoss  阅读(160)  评论(0编辑  收藏  举报