# 蓝桥杯-历届试题 剪格子

+--*--+--+
|10* 1|52|
+--****--+
|20|30* 1|
*******--+
| 1| 2| 3|
+--+--+--+

3 3
10 1 52
20 30 1
1 2 3

3

4 3
1 1 1 1
1 30 80 2
1 1 1 100

10

AC代码
`#include<stdio.h>#include<string.h>int num[15][15],visit[15][15];int i,j,n,m,sum,end=0;void Cutaws(int x,int y,int value,int count){    value+=num[x][y];    if(value==sum/2&&end!=1)    {        printf("%d\n",count+1);        memset(visit,1,sizeof(visit));        end=1;        return;    }    if(value>sum)    return;    if(y+1<n&&visit[x][y+1]==0)    {        visit[x][y+1]=1;        Cutaws(x,y+1,value,count+1);        visit[x][y+1]=0;    }    if(y-1>=0&&visit[x][y-1]==0)    {        visit[x][y-1]=1;        Cutaws(x,y-1,value,count+1);        visit[x][y-1]=0;    }    if(x+1<m&&visit[x+1][y]==0)    {        visit[x+1][y]=1;        Cutaws(x+1,y,value,count+1);        visit[x+1][y]=0;    }    if(y-1>=0&&visit[x-1][y]==0)    {        visit[x-1][y]=1;        Cutaws(x,y,value,count+1);        visit[x-1][y]=0;    }}int main(){    sum=0;    memset(num,0,sizeof(num));    memset(visit,0,sizeof(visit));    scanf("%d%d",&n,&m);    for(i=0;i<m;i++)    for(j=0;j<n;j++)    {       scanf("%d",&num[i][j]);       sum+=num[i][j];    }    visit[0][0]=1;    Cutaws(0,0,0,0);    return 0;}`

`#include<stdio.h>#include<string.h>struct array{   int flag;   int number;}num[15][15];int i,j,n,m,sum,end=0;void Cutaws(int x,int y,int value,int count){    int i,j,;    value+=num[x][y].number;    if(value==sum/2&&end!=1)    {        printf("%d\n",count+1);        for(i=0;i<15;i++)        for(j=0;j<15;j++)        num[i][j].flag=1;        end=1;        return;    }    if(value>sum)    return;    if(y+1<n&&num[x][y+1].flag==0)    {        num[x][y+1].flag=1;        Cutaws(x,y+1,value,count+1);        num[x][y+1].flag=0;    }    if(y-1>=0&&num[x][y-1].flag==0)    {        num[x][y-1].flag=1;        Cutaws(x,y-1,value,count+1);        num[x][y-1].flag=0;    }    if(x+1<m&&num[x+1][y].flag==0)    {        num[x+1][y].flag=1;        Cutaws(x+1,y,value,count+1);        num[x+1][y].flag=0;    }    if(x-1>=0&&num[x-1][y].flag==0)    {        num[x-1][y].flag=1;        Cutaws(x-1,y,value,count+1);        num[x-1][y].flag=0;    }}int main(){    sum=0;    for(i=0;i<15;i++)    for(j=0;j<15;j++)    num[i][j].flag=0;    scanf("%d%d",&n,&m);    for(i=0;i<m;i++)    for(j=0;j<n;j++)    {       scanf("%d",&num[i][j].number);       sum+=num[i][j].number;    }    num[0][0].flag=0;    Cutaws(0,0,0,0);    return 0;}`

0 0