1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include <iostream> #include <algorithm> #include <cstdio> #include <queue> #include <cstring> #define MAXN 10010001 #define INF 2000000000 struct Edge { int v, nx, w; }e[MAXN]; int min(int a, int b) { if (a < b) return a; else return b; } int head[MAXN], ecnt = 1, n, m, x, y, dep[MAXN], cur[MAXN], map[210][210], wb[210][210], r, k, cnt; int find[MAXN]; bool vis[MAXN], wh[MAXN]; void add(int f, int t, int w) { e[++ecnt] = (Edge) {t, head[f], w}; head[f] = ecnt; e[++ecnt] = (Edge) {f, head[t], 0}; head[t] = ecnt; } int g(int i, int j) { return (i - 1) * m + j; } std::queue <int> q; bool bfs(int s, int t) { memset(dep, 0x7f, sizeof(dep)); dep[s] = 0; while (!q.empty()) q.pop(); q.push(s); for (int i = 1; i <= n * m + 2; i++) { cur[i] = head[i]; } while (!q.empty()) { int u = q.front(); q.pop(); for (int i = head[u]; i; i = e[i].nx) { int to = e[i].v; if (dep[to] > INF && e[i].w) { dep[to] = dep[u] + 1; q.push(to); } } } if (dep[t] > INF) return 0; else return 1; } int dfs(int s, int t, int l) { if (!l || s == t) return l; int fl = 0, f; for (int i = cur[s]; i; i = e[i].nx) { int to = e[i].v; cur[s] = i; if (dep[to] == dep[s] + 1 && (f = dfs(to, t, min(l, e[i].w)))) { l -= f; fl += f; e[i].w -= f; e[i ^ 1].w += f; if (!l) break; } } return fl; } int Dinic(int s, int t) { int maxf = 0; while (bfs(s, t)) { maxf += dfs(s, t, INF); } return maxf; } void dfs(int now, int l) { if (vis[now]) return ; vis[now] = 1; if (wh[now] == l) { find[now] = 1; } for (int i = head[now]; i; i = e[i].nx) { if (e[i].w == l) dfs(e[i].v, l); } } int main() { scanf("%d%d", &n, &m); r = n * m + 1; k = n * m + 2; for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { char a; std::cin >> a; if (a == '.') { map[i][j] = 1; cnt++; } } } for (int i = 1; i <= n; i++) { wb[i][1] = wb[i - 1][1] ^ 1; } for (int i = 1; i <= n; i++) { for (int j = 2; j <= m; j++) { wb[i][j] = wb[i][j - 1] ^ 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (map[i][j] && wb[i][j]) { add(r ,g(i , j), 1); wh[g(i, j)] = 1; if (map[i + 1][j]) add(g(i, j), g(i + 1, j), 1); if (map[i - 1][j]) add(g(i, j), g(i - 1, j), 1); if (map[i][j + 1]) add(g(i, j), g(i, j + 1), 1); if (map[i][j - 1]) add(g(i, j), g(i, j - 1), 1); } if (map[i][j] && !wb[i][j]) { add(g(i, j), k, 1); } } } int tot = Dinic(r, k); if (cnt == tot) { puts("LOSE"); return 0; } puts("WIN"); dfs(r, 1); dfs(k, 0); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { if (find[g(i, j)]) { printf("%d %d\n", i, j); } } } return 0; }
|