题意:找出最多的形如“##”横着竖着都可以,明显的1X2矩形覆盖,直接按坐标和的奇偶来分为二分图。
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
#include<string>
#include<cstring>
#include<stack>
#include<queue>
#include<vector>
#include<cstdlib>
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define cl(a,b) memset(a,b,sizeof(a));
#define LL long long
#define P pair<int,int>
#define X first
#define Y second
#define pb push_back
#define fread(zcc) freopen(zcc,"r",stdin)
#define fwrite(zcc) freopen(zcc,"w",stdout)
using namespace std;
const int maxn=610;
const int inf=999999;
vector<int> G[maxn*maxn];
int matching[maxn*maxn];//因为对坐标是按行展开的编号,所以这里都要对应变大
bool vis[maxn*maxn];
int Nx;
bool dfs(int u){
int N=G[u].size();
for(int i=0;i<N;i++){
int v=G[u][i];
if(vis[v])continue;
vis[v]=true;
if(matching[v]==-1||dfs(matching[v])){
matching[v]=u;
return true;
}
}
return false;
}
int hungar(){
int ans=0;
cl(matching,-1);
for(int i=0;i<Nx;i++){
cl(vis,false);
if(dfs(i))ans++;
}
return ans;
}
char s[maxn][maxn];
int dir[][2]={1,0,0,1,-1,0,0,-1};
int main(){
int T,cas=1;
scanf("%d",&T);
while(T--){
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%s",s[i]);//图要全部读完,再去建立图
}
for(int i=0;i<n;i++){//建立二分图
for(int j=0;j<n;j++)if((i+j)%2==0&&s[i][j]==‘#‘){
for(int k=0;k<4;k++){
int xx=dir[k][0]+i;
int yy=dir[k][1]+j;
if(xx>=0&&xx<n&&yy>=0&&yy<n&&s[xx][yy]==‘#‘){
G[i*n+j].pb(xx*n+yy);
}
}
}
}
Nx=n*n;
printf("Case %d: %d\n",cas++,hungar());
for(int i=0;i<=n*n;i++)G[i].clear();
}
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-10-29 19:09:43