标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8338 Accepted Submission(s): 4796
/* ID: LinKArftc PROG: 1045.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const double e = exp(1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 20; struct Node { char cha; int idx, idy; } node[maxn][maxn]; int mp[maxn][maxn]; int n, uN, vN; int linker[maxn]; bool vis[maxn]; bool dfs(int u) { for (int v = 1; v <= vN; v ++) { if (!vis[v] && mp[u][v]) { vis[v] = true; if (linker[v] == -1 || dfs(linker[v])) { linker[v] = u; return true; } } } return false; } int hungry() { memset(linker, -1, sizeof(linker)); int ret = 0; for (int i = 1; i <= uN; i ++) { memset(vis, 0, sizeof(vis)); if (dfs(i)) ret ++; } return ret; } int main() { while (~scanf("%d", &n) && n) { for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) scanf(" %c", &node[i][j].cha); } uN = 0; for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { if (node[i][j].cha == ‘X‘) continue; if (j == 1 && node[i][j].cha == ‘.‘) uN ++; else if (node[i][j].cha == ‘.‘ && node[i][j-1].cha == ‘X‘) uN ++; node[i][j].idx = uN; } } vN = 0; for (int j = 1; j <= n; j ++) { for (int i = 1; i <= n; i ++) { if (node[i][j].cha == ‘X‘) continue; if (i == 1 && node[i][j].cha == ‘.‘) vN ++; else if (node[i][j].cha == ‘.‘ &&node[i-1][j].cha == ‘X‘) vN ++; node[i][j].idy = vN; } } memset(mp, 0, sizeof(mp)); for (int i = 1; i <= n; i ++) { for (int j = 1; j <= n; j ++) { if (node[i][j].cha == ‘X‘) continue; mp[node[i][j].idx][node[i][j].idy] = 1; } } printf("%d\n", hungry()); } return 0; }
标签:
原文地址:http://www.cnblogs.com/LinKArftc/p/4907711.html