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
| #include <bits/stdc++.h> using namespace std;
int nx[] = {-1, 0, 1, 0}; int ny[] = {0, 1, 0, -1};
int N, M; int a[105][105]; int c[105][105]; int cnt1, cnt2;
vector<int> G[20005]; bool vis[20005]; int m[20005];
bool tag[20005];
vector<pair<int, int> > Out;
bool find(int u) { if (vis[u]) return 0; vis[u] = 1; for (vector<int>::iterator it = G[u].begin(); it != G[u].end(); it++) { int v = *it; if (m[v] == 0 || find(m[v])) { m[u] = v, m[v] = u; return 1; } } return 0; }
bool dfs(int u) { if (vis[u]) return 0; vis[u] = 1; if (m[u] == 0) return 1; for (vector<int>::iterator it = G[m[u]].begin(); it != G[m[u]].end(); it++) { int v = *it; if (dfs(v)) return 1; } return 0; }
int main() { cin >> N >> M; for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { char ch; cin >> ch; if (ch == '#') continue; if ((i + j) & 1) c[i][j] = ++cnt1; else c[i][j] = ++cnt2 + 10000; } } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { if (c[i][j] == 0) continue; for (int k = 0; k < 4; ++k) { int ti = i + nx[k], tj = j + ny[k]; if (ti >= 1 && ti <= N && tj >= 1 && tj <= M) { if (c[ti][tj]) G[c[i][j]].push_back(c[ti][tj]); } } } } for (int i = 1; i <= cnt1; ++i) { memset(vis, 0, sizeof(vis)); find(i); } for (int i = 1; i <= N; ++i) { for (int j = 1; j <= M; ++j) { if (c[i][j] == 0) continue; memset(vis, 0, sizeof(vis)); if (dfs(c[i][j])) { Out.push_back(make_pair(i, j)); } } } printf("%d\n", Out.size()); for (int i = 0; i < (int)Out.size(); ++i) printf("%d %d\n", Out[i].first, Out[i].second); return 0; }
|