It's a network max_flow problem.But the meaning of the problem is difficultly to reading.The program may have many bugs.I Debug a whole afternoon.So sad a story.
It's relate to give one point.you should divide this point to two points(in_point and out_point ) to control the flow.And Don't forget you should use singular and plural.
The portal:http://acm.hdu.edu.cn/showproblem.php?pid=2732
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <memory.h>
const int MAXN = 100010;
const int MAXE = 400010;
const int INF = 0x3f3f3f3f;
struct Edge{
int to,next,cap,flow;
}edge[MAXE];
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init(){
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw = 0){
edge[tol].to = v;edge[tol].cap = w;edge[tol].next = head[u];
edge[tol].flow = 0;head[u] = tol ++;
edge[tol].to = u;edge[tol].cap = rw;edge[tol].next = head[v];
edge[tol].flow = 0;head[v] = tol ++;
}
int sap(int start,int end_point,int N){
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N){
if(u == end_point){
int Min = INF;
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i= pre[u];i!=-1;i=pre[edge[i^1].to]){
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i=cur[u];i != -1;i = edge[i].next){
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v] + 1 == dep[u]){
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag){
u = v;
continue;
}
int Min = N;
for(int i = head[u];i != -1;i=edge[i].next){
if(edge[i].cap-edge[i].flow && dep[edge[i].to] < Min){
Min = dep[edge[i].to];
cur[u] = i;
}
}
gap[dep[u]]--;
if(!gap[dep[u]]) return ans;
dep[u] = Min + 1;
gap[dep[u]] ++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}
char Map_rookies[25][25];
char Map_lizards[25][25];
int mat[25][25];
void Deal_with(){
int T,m,n,d,tot,t=1;
scanf("%d",&T);
while(T--){
//printf("%d\n",T);
init();
memset(Map_lizards,0,sizeof(Map_lizards));
memset(Map_rookies,0,sizeof(Map_rookies));
memset(mat,0,sizeof(mat));
scanf("%d %d",&n,&d);
for(int i=0;i<n;i++){
scanf("%s",Map_rookies[i]);
}
m = strlen(Map_rookies[0]);
tot = 0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(Map_rookies[i][j] > '0'){
mat[i][j] = ++tot;
addedge(tot*2-1,tot*2,Map_rookies[i][j] -'0');
}
}
}
int start = 0,end_point = tot * 2 + 1,numtot = tot * 2 + 2;
int cnt = 0;
for(int i=0;i<n;i++){
scanf("%s",Map_lizards[i]);
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(Map_lizards[i][j] == 'L'){
cnt ++;
addedge(start,mat[i][j]*2-1,1);
}
}
}
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(mat[i][j]){
for(int x = -d;x <= d;x ++){
for(int y = abs(x) - d ;y <= d - abs(x);y ++){
if(i+x < 0 || i+x >= n) continue ;
if(j+y < 0 || j+y >= m) continue ;
if(mat[i+x][j+y] == 0) continue;
if(x==0&&y==0)continue ;
addedge(mat[i][j]*2,mat[i+x][j+y]*2-1,INF);
}
}
if(i<d||j<d||n-i<=d||m-j<=d){
addedge(mat[i][j]*2,end_point,INF);
}
}
}
}
int ans = sap(start,end_point,numtot);
if(cnt == ans)
printf("Case #%d: no lizard was left behind.\n",t++);
else if(cnt == ans + 1)
printf("Case #%d: 1 lizard was left behind.\n",t++);
else
printf("Case #%d: %d lizards were left behind.\n",t++,cnt - ans);
}
}
int main(){
//freopen("a.in","r",stdin);
Deal_with();
return 0;
}