Antenna Placement
Antenna Placement
题目描述:
N * M的棋盘中,'o'代表障碍物,'*'代表空位,你可以使用1 * 2的多米诺骨牌放进空格,米牌可以重叠,也可以放在障碍物上,问最少使用多少牌使得放满所有空位
思路:
和上次那个棋盘问题很相似了,不过这次可以重叠了,那我们只需要计算出最大匹配值,然后就可以得出单放的空位是多少,最后需要放的牌的数量为:,num是空位的多少,pi是最大匹配数
#include<map> #include<set> #include<stack> #include<queue> #include<cmath> #include<cstdio> #include<string> #include<vector> #include<sstream> #include<cstring> #include<stdlib.h> #include<iostream> #include<algorithm> using namespace std; #define endl '\n' #define inf 0x3f3f3f3f #define MAX 100 + 50 #define mod 1000000007 #define lowbit(x) (x & (-x)) #define sd(n) scanf("%d",&n) #define sdd(n,m) scanf("%d %d",&n,&m) #define pd(n) printf("%d\n", (n)) #define pdd(n,m) printf("%d %d\n",n, m) #define sddd(n,m,z) scanf("%d %d %d",&n,&m,&z) #define io ios::sync_with_stdio(false); cin.tie(0); cout.tie(0) #define mem(a,b) memset((a),(b),sizeof(a)) typedef long long ll ; typedef unsigned long long ull; //不开longlong见祖宗!提交不看数据范围见祖宗! inline int IntRead(){char ch = getchar();int s = 0, w = 1;while(ch < '0' || ch > '9'){if(ch == '-') w = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){s = s * 10 + ch - '0';ch = getchar();}return s * w;} int t; int n, m; int white, black; char mp[MAX][MAX]; int tr[MAX][MAX]; bool p[MAX][MAX]; vector<int>v[10005]; bool vis[10005]; int link[10005]; bool dfs(int x){ for(int i = 0; i < v[x].size(); ++i){ int k = v[x][i]; if(!vis[k]){ vis[k] = 1; if(!link[k] || dfs(link[k])){ link[k] = x; return true; } } } return false; } void work(){ int ans = 0; for(int i = 1; i <= white; ++i){ mem(vis, 0); if(dfs(i))++ans; } cout<<white + black - ans<<endl; } void init(){ white = black = 0; mem(p, 0); mem(link, 0); mem(tr, 0); for(int i = 0; i <= n * m; ++i)v[i].clear(); } int main(){ sd(t); while (t--) { sdd(n, m); init(); for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ cin>>mp[i][j]; if(mp[i][j] == '*')p[i][j] = 1; } } for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ if(p[i][j]){ if((i + j) & 1)tr[i][j] = ++white; else tr[i][j] = ++black; } } } for(int i = 1; i <= n; ++i){ for(int j = 1; j <= m; ++j){ if(p[i][j] && (i + j) & 1){ if(tr[i][j - 1])v[tr[i][j]].push_back(tr[i][j - 1]); if(tr[i][j + 1])v[tr[i][j]].push_back(tr[i][j + 1]); if(tr[i + 1][j])v[tr[i][j]].push_back(tr[i + 1][j]); if(tr[i - 1][j])v[tr[i][j]].push_back(tr[i - 1][j]); } } } work(); } return 0; }