题解:

和上一题差不多

然后注意格式

代码:

#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
const int N=25;
using namespace std;
struct data{int to,ne,v;}e[500001];
int r,c,d,T,cnt,ans,cas,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;
}
void doit()
{
    scanf("%d%d",&r,&d);
    char ch[N];
    memset(mp,0,sizeof mp);
    memset(mark,0,sizeof mark);
    memset(fi,0,sizeof fi);
    memset(e,0,sizeof e);
    memset(q,0,sizeof q);
    memset(h,0,sizeof h); 
    cnt=1;
    ans=0;
    for (int i=1;i<=r;i++)
     {
           scanf("%s",ch);
           c=strlen(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("Case #%d: ",++cas);
    if (ans==0)printf("no ");else printf("%d ",ans);
     if (ans<2)puts("lizard was left behind.");
    else puts("lizards were left behind.");  
}
int main()
{
    scanf("%d",&T);
    while (T--)doit();
}

 

posted on 2018-01-23 17:42  宣毅鸣  阅读(109)  评论(0编辑  收藏  举报