A blockhouse is a small castle that has four openings through which to shoot. The four openings are facing North, East, South, and West, respectively. There will be one machine gun shooting through each opening.
Here we assume that a bullet is so powerful that it can run across any distance and destroy a blockhouse on its way. On the other hand, a wall is so strongly built that can stop the bullets.
The goal is to place as many blockhouses in a city as possible so that no two can destroy each other. A configuration of blockhouses is legal provided that no two blockhouses are on the same horizontal row or vertical column in a map unless there is at least one wall separating them. In this problem we will consider small square cities (at most 4×4) that contain walls through which bullets cannot run through.
The following image shows five pictures of the same board. The first picture is the empty board, the second and third pictures show legal configurations, and the fourth and fifth pictures show illegal configurations. For this board, the maximum number of blockhouses in a legal configuration is 5; the second picture shows one way to do it, but there are several other ways.
Your task is to write a program that, given a description of a map, calculates the maximum number of blockhouses that can be placed in the city in a legal configuration.
1 /* 2 二进制枚举 3 */ 4 #include <algorithm> 5 #include <cctype> 6 #include <cstdio> 7 #include <cstdlib> 8 #include <cstring> 9 #include <iostream> 10 #include <map> 11 #include <queue> 12 #include <set> 13 #include <stack> 14 #include <vector> 15 #define lson rt << 1, l, mid 16 #define rson rt << 1 | 1, mid + 1, r 17 using namespace std; 18 typedef long long ll; 19 typedef long double ld; 20 typedef unsigned long long ull; 21 int n; 22 int a[10][10], b[10][10]; 23 int judge(int t) 24 { 25 int f = n & 1; 26 memcpy(b, a, sizeof a); 27 int res = 0; 28 for (int i = 1; i <= n; i++) 29 for (int j = 1; j <= n; j++) 30 { 31 int idx = (i - 1) * n + j; 32 if (t & (1 << (idx - 1))) 33 { 34 if (b[i][j] != -1) 35 { 36 ++res; 37 b[i][j] = 1; 38 } 39 else 40 return 0; 41 } 42 } 43 for (int i = 1; i <= n; i++) 44 for (int j = 1; j <= n; j++) 45 { 46 if (b[i][j] == 1) 47 { 48 for (int k = i + 1; k <= n; k++) 49 if (b[k][j] == 1) 50 return 0; 51 else if (b[k][j] == -1) 52 break; 53 for (int k = i - 1; k >= 1; k--) 54 if (b[k][j] == 1) 55 return 0; 56 else if (b[k][j] == -1) 57 break; 58 for (int k = j + 1; k <= n; k++) 59 if (b[i][k] == 1) 60 return 0; 61 else if (b[i][k] == -1) 62 break; 63 for (int k = j - 1; k >= 1; k--) 64 if (b[i][k] == 1) 65 return 0; 66 else if (b[i][k] == -1) 67 break; 68 } 69 } 70 return res; 71 } 72 int main(int argc, char const *argv[]) 73 { 74 #ifndef ONLINE_JUDGE 75 freopen("in.txt", "r", stdin); 76 freopen("out.txt", "w", stdout); 77 #endif 78 ios::sync_with_stdio(false); 79 cin.tie(0); 80 cout.tie(0); 81 while (cin >> n && n) 82 { 83 for (int i = 1; i <= n; i++) 84 for (int j = 1; j <= n; j++) 85 { 86 char c; 87 cin >> c; 88 if (c == '.') 89 a[i][j] = 0; 90 else 91 a[i][j] = -1; 92 } 93 int k = 1 << (n * n); 94 int res = 0; 95 for (int i = 0; i < k; i++) 96 { 97 res = max(res, judge(i)); 98 } 99 cout << res << " "; 100 } 101 return 0; 102 }
二进制枚举
不过呢,这个题是在二分图专题里出来的,暴力a了之后去搜题解发现了用匹配的做法,之前做二分图匹配时一个很经典的行列建图
这个题些许不同,它有X点,因此我们考虑建图方式为:如果当前点左边儿为点则当前点的行依然为左点的行,否则当前点在新一行,列的方式类似
对每个点的新行列进行行列建图,求一边最大匹配即是题目所求最小点覆盖
1 /* 2 有X就新添一行或一列 3 最大匹配 4 */ 5 #include <algorithm> 6 #include <cctype> 7 #include <cstdio> 8 #include <cstdlib> 9 #include <cstring> 10 #include <iostream> 11 #include <map> 12 #include <queue> 13 #include <set> 14 #include <stack> 15 #include <vector> 16 #define lson rt << 1, l, mid 17 #define rson rt << 1 | 1, mid + 1, r 18 using namespace std; 19 typedef long long ll; 20 typedef long double ld; 21 typedef unsigned long long ull; 22 const int maxn = 110; 23 int llink[maxn], g[maxn][maxn]; 24 char s[maxn][maxn]; 25 bool visr[maxn]; 26 int col, row; 27 int c[maxn][maxn], r[maxn][maxn]; 28 int find(int u) 29 { 30 for (int i = 1; i <= col; i++) 31 { 32 if (g[u][i]) 33 { 34 if (!visr[i]) 35 { 36 visr[i] = 1; 37 if (llink[i] == -1 || find(llink[i])) 38 { 39 llink[i] = u; 40 return 1; 41 } 42 } 43 } 44 } 45 return 0; 46 } 47 int maxMatch() 48 { 49 int res = 0; 50 for (int i = 1; i <= row; i++) 51 { 52 memset(visr, 0, sizeof visr); 53 res += find(i); 54 } 55 return res; 56 } 57 int n; 58 int main(int argc, char const *argv[]) 59 { 60 #ifndef ONLINE_JUDGE 61 freopen("in.txt", "r", stdin); 62 freopen("out.txt", "w", stdout); 63 #endif 64 ios::sync_with_stdio(false); 65 cin.tie(0); 66 cout.tie(0); 67 while (cin >> n && n) 68 { 69 for (int i = 0; i < n; i++) 70 cin >> s[i]; 71 row = col = 0; 72 for (int i = 0; i < n; i++) 73 { 74 ++row; 75 for (int j = 0; j < n; j++) 76 { 77 if (s[i][j] == '.') 78 { 79 if (j && s[i][j - 1] == 'X') 80 r[i][j] = ++row; 81 else 82 r[i][j] = row; 83 } 84 } 85 } 86 for (int i = 0; i < n; i++) 87 { 88 ++col; 89 for (int j = 0; j < n; j++) 90 { 91 if (s[j][i] == '.') 92 { 93 if (j && s[j - 1][i] == 'X') 94 c[j][i] = ++col; 95 else 96 c[j][i] = col; 97 } 98 } 99 } 100 memset(g, 0, sizeof g); 101 for (int i = 0; i < n; i++) 102 for (int j = 0; j < n; j++) 103 if (s[i][j] == '.') 104 g[r[i][j]][c[i][j]] = 1; 105 memset(llink, -1, sizeof llink); 106 cout << maxMatch() << " "; 107 } 108 return 0; 109 }
巧妙建图-最小点覆盖