[BZOJ1443][JSOI2009]游戏Game

[JSOI2009]游戏Game

Description
[BZOJ1443][JSOI2009]游戏Game_第1张图片
Input
输入数据首先输入两个整数N,M,表示了迷宫的边长。 接下来N行,每行M个字符,描述了迷宫。
Output
若小AA能够赢得游戏,则输出一行”WIN”,然后输出所有可以赢得游戏的起始位置,按行优先顺序输出 每行一个,否则输出一行”LOSE”(不包含引号)。
Sample Input
3 3
.##

.#.#
Sample Output
WIN
2 3
3 2
HINT
对于100%的数据,有1≤n,m≤100。 对于30%的数据,有1≤n,m≤5。

Solution
首先黑白染色,得到一张二分图
考虑这个二分图的某一个最大匹配,从任意一个非匹配点开始,一定不存在 非匹配边->匹配边->非匹配边->…..->非匹配边的路径,也就是说不存在增广路,这意味着后手走一步先手总有办法走一步,那后手就必输无疑了
注意要找到所有可能的非匹配点,先dinic求得一个可行最大匹配,用dfs全部跑出来

Code

#include <bits/stdc++.h>
using namespace std;

#define rep(i, l, r) for (int i = (l); i <= (r); i++)
#define per(i, r, l) for (int i = (r); i >= (l); i--)
#define MS(_) memset(_, 0, sizeof(_))
#define MP make_pair
#define PB push_back
#define hash _hash
#define debug(...) fprintf(stderr, __VA_ARGS__)
typedef long long ll;
typedef pair<int, int> PII;
template<typename T> inline void read(T &x){
    x = 0; T f = 1; char ch = getchar();
    while (!isdigit(ch)) {if (ch == '-') f = -1; ch = getchar();}
    while (isdigit(ch))  {x = x * 10 + ch - '0'; ch = getchar();}
    x *= f;
}

const int INF = 0x7fffffff;
const int dx[5] = {0, -1, 0, 1, 0};
const int dy[5] = {0, 0, -1, 0, 1};
struct Node{
    int v, cap; Node *pr, *nxt;
}pool[30*10010], *tail = pool, *last[10010], *cur[10010];
int n, m, S, T;
int d[10010], q[10010*5];
int num[110][110], isblack[110][110], g[110][110], choose[10010], res[10010], vis[10010];
PII hash[10010];
inline void addedge(int u, int v, int cap){
    tail->v = v; tail->cap = cap; tail->pr = tail+1; tail->nxt = last[u]; last[u] = tail++;
    tail->v = u; tail->cap = 0; tail->pr = tail-1; tail->nxt = last[v]; last[v] = tail++;
}
inline bool check(int x, int y){ return 1<=x && x<=n && 1<=y && y<=m; }
inline bool BFS(int S, int T){ MS(d); int l, r, now;
    for (d[q[l = r = 1] = S] = 1; l <= r; l++)
        for (Node *p = last[now = q[l]]; p; p = p->nxt)
            if (!d[p->v] && p->cap>0) d[q[++r] = p->v] = d[now] + 1;
    return d[T];
}
inline int DFS(int u, int cap){
    if (u == T || cap == 0) return cap; int dt = cap;
    for (Node *&p = cur[u]; p && cap; p=p->nxt) if (p->cap && d[p->v]==d[u]+1){
        int t = DFS(p->v, min(cap, p->cap));
        cap -= t; p->cap -= t; p->pr->cap += t;
        if (!cap) break;
    }
    return dt-cap;
}
inline void Dinic(){ 
    while (BFS(S, T)) {
        for (int i = S; i <= T; i++) cur[i] = last[i];
        while(DFS(S, INF)); 
    }
}
inline void dfs(int u, int l, int cnt, int f){ vis[u] = 1;
    if (cnt % 2 == 0) res[u] = 1;
    if (!choose[u]) {
        for (Node *p = last[u]; p; p=p->nxt) 
            if (!vis[p->v] && p->v != S && p->v != T) dfs(p->v, l^1, cnt+1, f^1);
    }
    else{
        for (Node *p = last[u]; p; p=p->nxt) 
            if (!vis[p->v] && ((l&&p->cap==f^1)||(!l&&p->cap==f)) && p->v != S && p->v != T) dfs(p->v, l^1, cnt+1, f^1);
    }
}

int main(){
    read(n); read(m);
    int cnt = 1; rep(i, 1, n) rep(j, 1, m) num[i][j] = ++cnt;
    rep(i, 1, n) rep(j, 1, m) hash[num[i][j]] = MP(i, j);
    rep(i, 1, n) rep(j, 1, m) isblack[i][j] = !((i+j)&1);
    rep(i, 1, n){ char st[110];
        scanf("%s", st+1);
        rep(j, 1, m) g[i][j] = (st[j]=='#');
    }

    rep(i, 1, n) rep(j, 1, m) if (!g[i][j] && isblack[i][j])
        rep(k, 1, 4) if (!g[i+dx[k]][j+dy[k]] && check(i+dx[k], j+dy[k]))
            addedge(num[i][j], num[i+dx[k]][j+dy[k]], 1);
    S = 1; T = cnt+1;
    rep(i, 1, n) rep(j, 1, m) if (!g[i][j]){
        if (isblack[i][j]) addedge(S, num[i][j], 1);
        else addedge(num[i][j], T, 1);
    }
    Dinic();
    for (Node *p = last[S]; p; p=p->nxt) if (p->cap == 0) choose[p->v] = 1;
    for (Node *p = last[T]; p; p=p->nxt) if (p->pr->cap == 0) choose[p->v] = 1; 
    for (Node *p = last[S]; p; p=p->nxt) if (p->cap == 1) {MS(vis); dfs(p->v, 1, 0, 0);}
    for (Node *p = last[T]; p; p=p->nxt) if (p->pr->cap == 1) {MS(vis); dfs(p->v, 0, 0, 0);}

    int ans = 0;
    rep(i, S, T) ans += res[i];
    if (ans == 0) puts("LOSE"); else{
        puts("WIN");
        rep(i, S, T) if (res[i]) printf("%d %d\n", hash[i].first, hash[i].second);
    }
    return 0;   
}

你可能感兴趣的:(博弈,二分图匹配)