跳到主要內容

ICPC 2012 Latin America Regional – uva 12530

這題根本屌題,雖然看起來像是博弈題,不過可以用完美匹配來解決他

只要找出一個地區(連通)用黑白色來染每一格,然後只要是(二分圖)完美匹配代表先手怎麼放,後手怎麼放都OK

那就是完美匹配,不過如果一不是完美匹配,那就代表一定有一個位置放了之後後手穩輸

//============================================================================
// Name : Game of Tiles.cpp
// Date : 2013/3/18 下午2:17:15
// Author : GCA
//============================================================================
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <climits>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <cctype>
#include <utility>
using namespace std;
#ifdef ONLINE_JUDGE
#define ll "%lld"
#else
#define ll "%I64d"
#endif
typedef unsigned int uint;
typedef long long int Int;
#define Set(a,s) memset(a,s,sizeof(a))
#define Write(w) freopen(w,"w",stdout)
#define Read(r) freopen(r,"r",stdin)
#define Pln() printf("\n")
#define I_de(x,n)for(int i=0;i<n;i++)printf("%d ",x[i]);Pln()
#define De(x)printf(#x"%d\n",x)
#define For(i,x)for(int i=0;i<x;i++)
#define CON(x,y) x##y
#define Pmz(dp,nx,ny)for(int hty=0;hty<ny;hty++){for(int htx=0;htx<nx;htx++){\
printf("%d ",dp[htx][hty]);}Pln();}
#define M 55
#define PII pair<int,int>
#define PB push_back
#define oo INT_MAX
#define Set_oo 0x3f
#define Is_debug true
#define debug(...) if(Is_debug)printf("DEBUG: "),printf(__VA_ARGS__)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin();it!=(c).end();it++)
#define eps 1e-6
bool xdy(double x,double y){return x>y+eps;}
bool xddy(double x,double y){return x>y-eps;}
bool xcy(double x,double y){return x<y-eps;}
bool xcdy(double x,double y){return x<y+eps;}
int min3(int x,int y,int z){
int tmp=min(x,y);
return min(tmp,z);
}
int max3(int x,int y,int z){
int tmp=max(x,y);
return max(tmp,z);
}
vector<int> mz[M*M],enable,benable;
int have[M*M],bhave[M*M];
int cmap[M][M];
int nx,ny;
int color[M][M];
int mx[M*M],my[M*M];
int d[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
void dfs_color(int x,int y,int c,int depth){
for(int i=0;i<4;i++){
int newx=x+d[i][0],newy=y+d[i][1];
if(newx>=nx||newy>=ny||newx<0||newy<0||cmap[newx][newy]=='X')continue;
int tmp=nx*newy+newx;
if(c==0){
color[x][y]=1;
mz[nx*y+x].PB(tmp);
// mz[tmp].PB(nx*y+x);
if(!have[nx*y+x]){
enable.PB(nx*y+x);
have[nx*y+x]=1;
}
if(!bhave[tmp]){
benable.PB(tmp);
bhave[tmp]=1;
}
// debug("add %d %d %d\n",nx*y+x,tmp,depth);
if(color[newx][newy])continue;
dfs_color(newx,newy,1,depth+1);
}
else{
color[x][y]=2;
if(color[newx][newy])continue;
dfs_color(newx,newy,0,depth+1);
}
}

}
int vis[M*M];
bool dfs2(int x,int depth){
FOR(it,mz[x]){
int y=(*it);
// if(x==63)printf("63y %d %d\n",y,depth);
if(!vis[y]){
vis[y]=true;
if(my[y]==-1||dfs2(my[y],depth+1)){
mx[x]=y;
my[y]=x;
return true;
}
}
}
return false;
}
bool dfsAndB(int x,int y){
int cnt=0;
Set(have,0);
Set(bhave,0);
enable.clear();
benable.clear();
Set(mx,-1);
Set(my,-1);

enable.PB(nx*y+x);
have[nx*y+x]=1;
dfs_color(x,y,0,0);

int numy=benable.size();
int numx=enable.size();
if(numx!=numy)return false;
FOR(it,enable){
Set(vis,0);
if(dfs2((*it),0))cnt++;
}
// printf("numx %d numy %d cnt%d\n",numx,numy,cnt);
// FOR(it,enable)printf("%d %d\n",*it,mx[*it]);

return (cnt==numx);

}
bool solve(){

for(int i=0;i<ny;i++){
for(int j=0;j<nx;j++){
if(!color[j][i]&&cmap[j][i]!='X'){
// debug("=============_______===============\n");
if(!dfsAndB(j,i))return true;
}
}
}
return false;
}
int main() {
ios_base::sync_with_stdio(0);
while(~scanf("%d%d%*c",&ny,&nx)){
Set(cmap,0);
Set(color,0);
for(int i=0;i<ny;i++){
for(int j=0;j<nx;j++){
cmap[j][i]=getchar();
}
getchar();
}
if(solve())printf("1\n");
else printf("2\n");

for(int i=0;i<nx*ny+1;i++)mz[i].clear();


}

















}

留言