1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <stdio.h>
int a[10][10];//Save value
int b[10][10];//Tagging arrays
int count=100;
int m,n,sum=0;
int dx[4]={0,1,0,-1};
int dy[4]={1,0,-1,0};
void f(int s,int i,int j,int bs)
{ //s-> current and i,j coordinates of the current point , bs current number of steps (number of grids)
if(s==sum/2&&count>bs)count=bs;//Determine if the answer is updated
else{
for(int k=0;k<4;k++){
int x=dx[k]+i;
int y=dy[k]+j;//Boundary judgment and pruning
if(b[x][y]==1&&i>=0&&i<n&&j>=0&&j<m&&(s+a[x][y])<=sum/2){
b[x][y]=0; //At this point, a[0][0] is only used for concatenation, so neither the grid nor the value is increased.
if(x==0&&y==0) f(s,x,y,bs);
else f(s+a[x][y],x,y,bs+1);
b[x][y]=1;//Elimination of markers
}
}
}
}
int main()
{int i,j,tem;
scanf("%d%d",&m,&n);//Input width and height
for(i=0;i<n;i++)//Note that the pit point is entered first in the vertical coordinate m and then in the horizontal coordinate
for(j=0;j<m;j++)
{scanf("%d",&a[i][j]);//Value
sum+=a[i][j]; b[i][j]=1;//Initializing the tag array
}
if(sum%2==1)printf("0");//Can pre-judgment be divided into two parts
else{ f(a[0][0],0,0,1);
if(count!=100) printf("%d\n",count);
else printf("0\n");
}
return 0;
}
|