题目链接:
POJ 2226 Muddy Fields
题意:
和POJ 3041 类似。只不过每次只可以清除一列或一行中连续的点,问最少的清除次数。
分析:
不再将每行和每列看成二分图的点,而是将一行中连续的点和一列中连续的点看成二分图的点,建好图下面就是套路了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <climits>
#include <cmath>
#include <ctime>
#include <cassert>
#include <map>
#include <string>
#define IOS ios_base::sync_with_stdio(0); cin.tie(0);
using namespace std;
typedef long long ll;
const int MAX_N = 3000;int n, m, total, cntx, cnty;
int head[MAX_N], vis[MAX_N], match[MAX_N], x[MAX_N][MAX_N], y[MAX_N][MAX_N];
char s[MAX_N][MAX_N];struct Edge{int to, next;Edge() {}Edge(int _to, int _next) : to(_to), next(_next){}
}edge[MAX_N*MAX_N];inline void AddEdge(int from, int to)
{edge[total].to = to;edge[total].next = head[from];head[from] = total++;
}inline bool dfs(int u)
{for(int i = head[u]; i != -1; i = edge[i].next){int to = edge[i].to;if(!vis[to]){vis[to] = 1;if(match[to] == -1 || dfs(match[to])){match[to] = u;return true;}}}return false;
}inline void hungary()
{int ans = 0;for(int i = 0; i < cntx; ++ i){memset(vis,0,sizeof(vis));if(dfs(i)) ans++;}cout << ans << endl;
}int main()
{freopen("E.in", "r", stdin);IOS;while(cin >> n >> m){memset(head, -1, sizeof(head));memset(match, -1, sizeof(match));cntx = cnty = total = 0;for(int i = 0; i < n; ++ i){cin >> s[i];}for(int i = 0; i < n; i++){for(int j = 0; j < m;){if(s[i][j] == '*'){int end = m-1;for(int k = j; k < m; k++){if( s[i][k] == '.'){end = k-1;break;}}for(int k = j; k <= end; k++){x[i][k] = cntx;}cntx++;j = end + 1;}else {j++;}}}for(int j = 0; j < m; j++){for(int i = 0; i < n;){if(s[i][j] == '*'){int end = n-1;for(int k = i; k < n; k++){if( s[k][j] == '.'){end = k-1;break;}}for(int k = i; k <= end; k++){y[k][j] = cnty;}cnty++;i = end + 1;}else {i++;}}}for(int i = 0; i < n; i++){for(int j = 0; j < m; j++){if(s[i][j] == '*'){AddEdge(x[i][j], y[i][j]);}}}hungary();}return 0;
}