解析:
很容易看出是二分图最大独立点集
对格子进行黑白染色。。。
连原点。连汇点。。。中间有能到达的连边无穷。。。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define inf 1000000000
const int dx[8]={1,1,2,2,-1,-1,-2,-2};
const int dy[8]={2,-2,1,-1,2,-2,1,-1};
int n,nn=0,mm=0,l=0,ans=0;
int tmp[210][210];
struct node
{
int u,v,c,next;
}edge[400000];
int head[400000],d[40000],sumd[400000];
int s,t;
void read()
{
freopen("TJOI.in","r",stdin);
freopen("TJOI.out","w",stdout);
}
void add(int u,int v,int c)
{
//printf("%d %d %d\n",u,v,c);
edge[l].u=u;
edge[l].v=v;
edge[l].c=c;
edge[l].next=head[u];
head[u]=l++;
edge[l].u=v;
edge[l].v=u;
edge[l].c=0;
edge[l].next=head[v];
head[v]=l++;
}
bool check(int x,int y)
{
if((1<=x)&&(x<=n)&&(y>=1)&&(y<=n)&&(tmp[x][y]))return 1;else return 0;
}
int sap(int u,int flow)
{
if(u==t)return flow;
int sum=0;
for(int i=head[u];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(edge[i].c && d[u]==d[v]+1)
{
int tt=sap(v,min(edge[i].c,flow-sum));
sum+=tt;
edge[i].c-=tt;edge[i^1].c+=tt;
if(sum==flow)return sum;
if(d[0]>=ans+2)return sum;
}
}
sumd[d[u]]--;
if(sumd[d[u]]==0)d[0]=ans+2;
++sumd[++d[u]];
return sum;
}
void work()
{
scanf("%d\n",&n);
memset(tmp,0,sizeof(tmp));
for(int i=1;i<=n;++i)
{
for(int j=1;j<=n;++j)
if(getchar()=='0')tmp[i][j]=1,++ans;
getchar();
}
t=n*n+1;
memset(head,-1,sizeof(head));
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
{
int num=(i-1)*n+j;
if(tmp[i][j])
{
if((i+j)%2)add(0,num,1); else add(num,t,1);
if((i+j)%2)
{
for(int k=0;k<8;k++)
{
int nx=i+dx[k];
int ny=j+dy[k];
int cnt=(nx-1)*n+ny;
if(check(nx,ny))
add(num,cnt,inf);
}
}
}
}
int ret=0;
sumd[0]=ans+2;
while(d[0]<=ans+1)
ret+=sap(0,inf);
printf("%d\n",ans-ret);
}
int main()
{
read();
work();
return 0;
}