题解:

网络流

把点拆开来

代码:

#include<bits/stdc++.h>
const int N=25;
using namespace std;
struct data{int to,ne,v;}e[500001];
int r,c,d,cnt=1,ans,mp[N][N],mark[N][N],q[802],h[802],fi[802];
void ins(int u,int v,int w)
{
    cnt++;e[cnt].to=v;
    e[cnt].ne=fi[u];
    fi[u]=cnt;
    e[cnt].v=w;
}
void insert(int u,int v,int w)
{
    ins(u,v,w);
    ins(v,u,0);
}
int judge(int x1,int y1,int x2,int y2)
{
    if (((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2))<=(d*d)&&mp[x1][y1]&&mp[x2][y2])
     return 1;
    return 0;
}
void build()
{
    for (int x1=1;x1<=r;x1++)
     for (int y1=1;y1<=c;y1++)
      for (int x2=x1-d;x2<=x1+d;x2++)
       for (int y2=y1-d;y2<=y1+d;y2++)
    if (judge(x1,y1,x2,y2)&&(x1!=x2||y1!=y2))
     insert(mark[x1][y1]+400,mark[x2][y2],1e9); 
    for (int i=1;i<=r;i++)
     for (int j=1;j<=c;j++)
      if (mp[i][j])insert(mark[i][j],mark[i][j]+400,mp[i][j]);
} 
int bfs()
{
    memset(h,-1,sizeof(h));
    int t=0,w=1,now;
    q[0]=h[0]=0;
    while (t<w)
     {
        now=q[t++];
        for (int i=fi[now];i;i=e[i].ne)
         if (e[i].v&&h[e[i].to]==-1)
          {
            h[e[i].to]=h[now]+1;
            q[w++]=e[i].to; 
          }
     }
    if (h[801]==-1)return 0;
    return 1;
}
int dfs(int x,int f)
{
    if (x==801)return f;
    int used=0,w;
    for (int i=fi[x];i;i=e[i].ne)
     if (e[i].v&&h[e[i].to]==h[x]+1)
      {
        w=f-used;w=dfs(e[i].to,min(w,e[i].v));
        e[i].v-=w;e[i^1].v+=w;
        used+=w;
        if(used==f)return f;
      }
    if (!used)h[x]=-1;
    return used;
}
int main()
{
    scanf("%d%d%d",&r,&c,&d);
    char ch[N];
    for (int i=1;i<=r;i++)
     {
           scanf("%s",ch);
           for (int j=1;j<=c;j++)mp[i][j]=ch[j-1]-'0';
     }
    int tot=0;
    for (int i=1;i<=r;i++)
     for (int j=1;j<=c;j++)
      {
          tot++;
        mark[i][j]=tot;
       } 
    for (int i=1;i<=r;i++)
     {
           scanf("%s",ch);
           for(int j=1;j<=c;j++)
         if (ch[j-1]=='L'){insert(0,mark[i][j],1);ans++;}
     }
    for (int i=1;i<=d;i++)
     for (int j=d+1;j<=r-d;j++)
      {
        insert(mark[j][i]+400,801,1e9);
        insert(mark[j][c-i+1]+400,801,1e9);
      }
    for (int i=1;i<=d;i++)
     for (int j=1;j<=c;j++)
      {
        insert(mark[i][j]+400,801,1e9);
        insert(mark[r-i+1][j]+400,801,1e9);
      }
    build();
    while (bfs())ans-=dfs(0,1e9);
    printf("%d",ans);
    return 0;
}

 

posted on 2018-01-22 18:48  宣毅鸣  阅读(100)  评论(0编辑  收藏  举报