我调试了以下程序。它找到了正确的结果,但是递归函数mmat
中的ri
被更改,而没有被代码pmat[q][h].ptes=NULL;
和i
的这一部分重新分配,最终结果是所有非预先分配的指针为null。全局变量mat **mmat
遇到了同样的问题,而不是在ri
中通过引用传递它。有人知道原因吗?
typedef struct tes_{
struct seg_{
char c;
int v;
}seg[2];
}tes;
typedef struct mat_{
tes *ptes;
int rot;
} mat;
int max=0;
int main()
{
FILE *f; char frase[M], buff[M]; int n,i,j,r,c,q=0,z=0,e=0,*pos,*esc; tes *vtes; mat **pmat, **mmat;
printf("Inserire nome file input tessere: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,50,f); sscanf(frase,"%d",&n);
vtes=malloc(n*sizeof(tes));
for (i=0;i<n;i++) {fgets(frase,M,f); sscanf(frase,"%c %d %c %d",&(vtes[i].seg[0].c),&(vtes[i].seg[0].v),&(vtes[i].seg[1].c),&(vtes[i].seg[1].v));}
fclose(f);
printf("Inserire nome file input scacchiera: "); scanf("%s",frase);
f=fopen(frase,"r");
if (f==NULL) exit(1);
fgets(frase,M,f); sscanf(frase,"%d %d",&r,&c);
esc=(int*)malloc(r*c*sizeof(int));
pmat=(mat **)malloc(r*sizeof(mat *));
mmat=(mat **)malloc(r*sizeof(mat *));
for (i=0;i<r;i++) {pmat[i]=(mat *)malloc(c*sizeof(mat)); mmat[i]=(mat *)malloc(c*sizeof(mat));}
for (i=0;i<r;i++) {
fgets(frase,M,f); q=0;
for (j=0;j<c;j++) {
z=0;
while (frase[q]!='/') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; z=0;
if (atoi(buff)==-1) pmat[i][j].ptes=NULL;
else {pmat[i][j].ptes=&(vtes[atoi(buff)]); esc[e]=atoi(buff); e++;}
while (frase[q]!=' ' || frase[q]=='\n' || frase[q]=='\0') {
buff[z]=frase[q]; q++; z++;
}
q++; buff[z]='\0'; pmat[i][j].rot=atoi(buff);
}
}
pos=(int *)malloc((n-e)*sizeof(int)); q=0;
for(i=0;i<n;i++) { // pos contiene tutte le tessere inseribili
for (j=0;j<e;j++) if(esc[j]==i) break;
if (j>=e) {pos[q]=i; q++;}
}
ri(pmat,&mmat,vtes,pos,n-e,r,c);
printf("Max valore %d\n",max);
for (i=0;i<r;i++) {
for (j=0;j<c;j++) {
printf("%c %d // %c %d\t",mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(0+mmat[i][j].rot)%2].v,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].c,mmat[i][j].ptes->seg[(1+mmat[i][j].rot)%2].v);
}
printf("\n");
}
return 0;
}
void ri(mat **pmat, mat ***mmat, tes *vtes, int *pos, int i, int r, int c){
int j,q,h,z,*pos2;
if (i==0) {
if(check(pmat,vtes,r,c)) {
*mmat=pmat;
}
return;
}
for (j=0;j<i;j++) {
for (q=0;q<r;q++) {
for (h=0;h<c;h++) {
if (pmat[q][h].ptes==NULL) break;
}
if (h<c) break;
}
if (q==r && h==c) return;
pos2=malloc((i-1)*sizeof(int));
for (z=0;z<i;z++){
if (z<j) pos2[z]=pos[z];
else if (z>j) pos2[z-1]=pos[z];
}
pmat[q][h].ptes=&(vtes[pos[j]]);
pmat[q][h].rot=0;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
pmat[q][h].rot=1;
ri(pmat,mmat,vtes,pos2,i-1,r,c);
free(pos2);
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
pmat[q][h].ptes=NULL;
if ((*mmat)[0][1].ptes==NULL) {
printf("prob");
}
}
}
int check(mat **pmat,tes *vtes, int r, int c){
int i,j, cont=0;
for (i=0;i<r;i++) {
for (j=0;j<c-1;j++) {
if (pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].c!=pmat[i][j+1].ptes->seg[(0+pmat[i][j+1].rot)%2].c) break;
}
if (j==c-1) {for (j=0;j<c;j++) cont+=pmat[i][j].ptes->seg[(0+pmat[i][j].rot)%2].v;}
}
for (i=0;i<c;i++) {
for (j=0;j<r-1;j++) {
if (pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].c!=pmat[j+1][i].ptes->seg[(1+pmat[j+1][i].rot)%2].c) break;
}
if (j==r-1) {for (j=0;j<r;j++) cont+=pmat[j][i].ptes->seg[(1+pmat[j][i].rot)%2].v;}
}
if (cont>max) {
max=cont;
return 1;
}
return 0;
}