Description
给定一个01矩阵,其中你可以在0的位置放置攻击装置。每一个攻击装置(x,y)都可以按照“日”字攻击其周围的 8个位置(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-1),(x-1,y+2),(x-2,y+1), (x+1,y+2),(x+2,y+1)
求在装置互不攻击的情况下,最多可以放置多少个装置。
Input
第一行一个整数N,表示矩阵大小为N*N。接下来N行每一行一个长度N的01串,表示矩阵。
Output
一个整数,表示在装置互不攻击的情况下最多可以放置多少个装置。
Sample Input
3
010
000
100
Sample Output
4
感想
这题我一开始看题,走日字,于是我很傻逼地吧八个点画了出来,觉得不对啊,围出的图形不是日啊。。
然后发现是我傻逼
题解
这题一看就是图论啊。。
大概是网络流。。
我们想到暴力乱建图。。
就向他到的点连一条边。。
最小割?肯定不行。。
但大概可以这个思路
我们观察到图肯定是对称的。。
将有用点*2-最大流,最后除以2就是答案了
和我以前做的连连看有点像,对称图就是神奇
10S惊险卡过
做完看了下题解,发现大家是二分图匹配,囧
确实是啊,好像我的也是二分图。。
但是最大独立集怎么求我早就忘了
CODE:
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
const int N=205*205*2;//有多少个点
const int MAX=1<<30;
int n;
char ss[205][205];
int X[8]={-1,-2,+1,+2,-1,-2,+1,+2};
int Y[8]={-2,-1,-2,-1,+2,+1,+2,+1};
/*(x-1,y-2),(x-2,y-1),(x+1,y-2),(x+2,y-1),(x-1,y+2),(x-2,y+1), (x+1,y+2),(x+2,y+1)*/
struct qq{int x,y,z,last;}s[N*8];
int num,last[N];
int st,ed;
void init (int x,int y,int z)
{
num++;
s[num].x=x;s[num].y=y;s[num].z=z;
s[num].last=last[x];
last[x]=num;
swap(x,y);z=0;
num++;
s[num].x=x;s[num].y=y;s[num].z=z;
s[num].last=last[x];
last[x]=num;
}
int P (int x,int y){return (x-1)*n+y;}
int h[N];
bool Bt ()
{
memset(h,-1,sizeof(h));h[st]=1;
queue<int> q;
q.push(st);
while (!q.empty())
{
int x=q.front();q.pop();
for (int u=last[x];u!=-1;u=s[u].last)
{
int y=s[u].y;
if (s[u].z>0&&h[y]==-1)
{
h[y]=h[x]+1;
q.push(y);
}
}
}
return h[ed]!=-1;
}
int mymin (int x,int y){return x<y?x:y;}
int dfs (int x,int f)
{
if (x==ed) return f;
int s1=0;
for (int u=last[x];u!=-1;u=s[u].last)
{
int y=s[u].y;
if (s[u].z>0&&h[y]==h[x]+1&&s1<f)
{
int lalal=dfs(y,mymin(s[u].z,f-s1));
s1+=lalal;
s[u].z-=lalal;
s[u^1].z+=lalal;
}
}
if (s1==0) h[x]=-1;
return s1;
}
int main()
{
num=1;memset(last,-1,sizeof(last));
scanf("%d",&n);st=n*n*2+1;ed=st+1;
for (int u=1;u<=n;u++)
scanf("%s",ss[u]+1);
int ans=0;
for (int u=1;u<=n;u++)
{
for (int i=1;i<=n;i++)
{
if (ss[u][i]=='1') continue;
ans+=2;init(st,P(u,i),1);init(P(u,i)+n*n,ed,1);
for (int j=0;j<8;j++)
{
int fx=u+X[j],fy=i+Y[j];
if (fx>=1&&fx<=n&&fy>=1&&fy<=n&&ss[fx][fy]=='0')
init(P(u,i),P(fx,fy)+n*n,MAX);
}
}
}
/*for (int u=2;u<=num;u+=2)
{
printf("%d %d %d\n",s[u].x,s[u].y,s[u].z);
system("pause");
}*/
while (Bt()) ans=ans-dfs(st,MAX);
printf("%d\n",ans/2);
return 0;
}