recursion - 2D array Maze solve in C -
i have maze represented square array of integers. have find least-cost path (sum of integers) through maze, moving orthogonally.
12323 12323 11232 21111
for instance, track through maze above 1's:
x2323 x2323 xx232 2xxxx
i wrote recursive function find path, return 0 instead of number of steps.
i set breakpoint , find out line below executes (comparison true) when array location matches num.
if(*((int*)table)!=num) return 0;
i think have problems int**
code:
case 3:// main. arrtask3 initialized get2darray. printf("enter size of array\n"); scanf("%d",&arrtask3size); arrtask3size=getarrsize(arrtask3size); get2darr((int**)arrtask3, arrtask3size, 0, 0); num=*((int*)arrtask3); start=(int**)arrtask3; finish=(int**)(arrtask3)+arrtask3size*arrtask3size; task3he(num, arrtask3size, arrtask3, finish, start, 0, 's'); void get2darr(int** arr2d,int size,int row, int col){ int num; if(row<size){ if(col<size){ printf("enter element @ cell [%d][%d]\n",row,col); scanf("%d",&num); *((int*)arr2d+row*size+col)=num; get2darr(arr2d, size, row, col+1); } else get2darr(arr2d, size, row+1, 0); } } int task3he(int num,int n ,int **table,int **finish,int **start,int count,char prvstep){ int tempup,tempdo,templ,tempr; if((int)(table-finish)==0&&num==*((int*)table)) return count; if((int)(table-finish)==0) return 0; if((int)table<(int)start||(int)table>(int)finish) return 0; if(*((int*)table)!=num) return 0; if(prvstep=='u'){ tempup=task3he(num,n, (int**)table-n, finish, start, count+1,'u'); if (tempup!=0) return 1; templ=task3he(num,n, (int**)table-1, finish, start, count+1,'l'); if (templ!=0) return 1; tempr=task3he(num,n, (int**)table+1, finish, start, count+1,'r'); if (tempr!=0) return 1; } if(prvstep=='d'){ tempdo=task3he(num,n, (int**)table+n, finish, start, count+1,'d'); if (tempdo!=0) return 1; templ=task3he(num,n, (int**)table-1, finish, start, count+1,'l'); if (templ!=0) return 1; tempr=task3he(num,n, (int**)table+1, finish, start, count+1,'r'); if (tempr!=0) return 1; } if(prvstep=='l'){ tempup=task3he(num,n, (int**)table-n, finish, start, count+1,'u'); if (tempup!=0) return 1; tempdo=task3he(num,n, (int**)table+n, finish, start, count+1,'d'); if (tempdo!=0) return 1; templ=task3he(num,n, (int**)table-1, finish, start, count+1,'l'); if (templ!=0) return 1; } if(prvstep=='r'){ tempup=task3he(num,n, (int**)table-n, finish, start, count+1,'u'); if (tempup!=0) return 1; tempdo=task3he(num,n, (int**)table+n, finish, start, count+1,'d'); if (tempdo!=0) return 1; tempr=task3he(num,n, (int**)table+1, finish, start, count+1,'r'); if (tempr!=0) return 1; } if(prvstep=='s'){ tempup=task3he(num,n, (int**)table-n, finish, start, count+1,'u'); if (tempup!=0) return 1; tempdo=task3he(num,n, (int**)table+n, finish, start, count+1,'d'); if (tempdo!=0) return 1; templ=task3he(num,n, (int**)table-1, finish, start, count+1,'l'); if (templ!=0) return 1; tempr=task3he(num,n, (int**)table+1, finish, start, count+1,'r'); if (tempr!=0) return 1; } return 0; }
take @ immediate problem:
if(*((int*)table)!=num) return 0;
table 2-d int array. cast 1-d int array. dereference if were 1-d array. gives address of table[0], top row. compare against parameter num.
i'm not sure num is, since haven't bothered document program or otherwise explain interface. however, english usage suggests num small positive integer. means not numerically equal memory address. "not equal" comparison true.
i can't fix you, since i'm unclear on how program supposed work. recommend learn building blocks of programming before tackle involved.
among other things, learn how use basic data types naturally, rather casting them other types -- 1 commenter mentioned, type-cast means have innate error in design. instance, @ comparison:
(int)(table-finish)==0
in words, trying statement? semantically, compares starting addresses of pair of 2-d arrays. that straightforward
table == finish
for reason, you've found necessary subtract addresses, convert difference integer (it's integer type), , compare converted difference 0. used three operations instead of straightforward one.
also, learn incremental programming: write few lines, debug them, , not go on until you're sure work. code above has many problems, making hard read , hard debug. especially, should not have written of code when had not yet debugged ability reference single array location. commenter noted seem overwrite array size -- if can't input correctly, how rest of program supposed work?
Comments
Post a Comment