题意:给定一个图,L代表陆地,W代表水,C表示不确定,问你最多有多少岛。
析:首先给定的L周围必须是是W,只有这样才是最优的,因为如果是L,那么还得有另外的W来包围,不是最优的,那么剩下的就剩下C了,因为要是L多,那么肯定是一个岛屿只有一个L,这样是最优的,并且周围都是W,所以可以把C看成一个点,然后向周围上下左右连边,如果周围存在C,那么就连一条,最后求一个最大独立集就OK了,二分图的最大独立集等于二分图的顶点数 - 二分图的最大匹配。也就是求二分匹配。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <iostream> #include <cstring> #include <set> #include <queue> #include <algorithm> #include <vector> #include <map> #include <cctype> #include <cmath> #include <stack> #include <sstream> #include <list> #include <assert.h> #include <bitset> #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define fi first #define se second #define pb push_back #define sqr(x) ((x)*(x)) #define ms(a,b) memset(a, b, sizeof a) #define sz size() #define pu push_up #define pd push_down #define cl clear() #define all 1,n,1 #define FOR(i,x,n) for(int i = (x); i < (n); ++i) #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair<int, int> P; const int INF = 0x3f3f3f3f; const double inf = 1e20; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 50 + 50; const LL mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c) { return r >= 0 && r < n && c >= 0 && c < m; } struct Edge{ int to, next; }; Edge edge[maxn*maxn]; int head[maxn*maxn], cnt; int G[maxn][maxn]; char s[maxn][maxn]; void addEdge(int u, int v){ edge[cnt].to = v; edge[cnt].next = head[u]; head[u] = cnt++; } bool vis[maxn][maxn]; bool used[maxn*maxn]; int match[maxn*maxn]; void dfs(int r, int c){ for(int i = 0; i < 4; ++i){ int x = r + dr[i]; int y = c + dc[i]; if(vis[x][y] || !is_in(x, y)) continue; vis[x][y] = 1; if(s[x][y] == 'L') dfs(x, y); else s[x][y] = 'W'; } } bool dfs(int u){ used[u] = true; for(int i = head[u]; ~i; i = edge[i].next){ int v = edge[i].to, w = match[v]; if(w < 0 || !used[w] && dfs(w)){ match[u] = v; match[v] = u; return true; } } return false; } int main(){ scanf("%d %d", &n, &m); for(int i = 0; i < n; ++i) scanf("%s", s[i]); int ans = 0; FOR(i, 0, n) FOR(j, 0, m) if(!vis[i][j] && s[i][j] == 'L'){ vis[i][j] = 1; dfs(i, j); ++ans; } ms(G, -1); ms(head, -1); cnt = 0; int idx = 0; FOR(i, 0, n) FOR(j, 0, m) if(s[i][j] == 'C') G[i][j] = idx++; FOR(i, 0, n) FOR(j, 0, m) if(i+j&1&&~G[i][j]){ for(int k = 0; k < 4; ++k){ int x = dr[k] + i; int y = dc[k] + j; if(is_in(x, y) && ~G[x][y]){ addEdge(G[i][j], G[x][y]); addEdge(G[x][y], G[i][j]); } } } ms(match, -1); int cnt = 0; for(int i = 0; i < idx; ++i) if(match[i] < 0){ ms(used, 0); if(dfs(i)) ++cnt; } printf("%d\n", ans += idx - cnt); return 0; }