Red Huang

Red Huang

ICPC 2012 Latin America Regional - uva 12530

This question is really difficult. Although it looks like a game theory problem, it can actually be solved using perfect matching.

All we need to do is find a region (connected) and color each cell with black and white. Then, if it is a perfect matching (bipartite graph), it means that no matter how the first player places their tiles, the second player can always place their tiles in a way that guarantees a win.

That is the perfect matching. However, if it is not a perfect matching, it means that there must be a position where the second player will definitely lose after placing their tile.

//============================================================================  
// 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();  
  
  
    }  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
  
}  

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.