Hey,
I've made a program to find the shortest path in a maze.. doesn't work though. Gives the weirdest error (NEVER seen this one before) on linux.. haven't tested it on windows. Help me!:
I've used a stack to keep a list of what I haven't gone through in the maze yet, and the history is used to know what i've already visited. I tried linking my stack both ways, too.
Oh.. there's the image.
HELP!
I've made a program to find the shortest path in a maze.. doesn't work though. Gives the weirdest error (NEVER seen this one before) on linux.. haven't tested it on windows. Help me!:
Code:
// Maze is :
//
// __________
// |O|X|X| | | |
// | | | | |X| |
// | | | |X|X| |
// | | | |X|X| |
// | |X|X|X|X| |
// |_ |_ |_| _|_|W|
//
// where X means that passing is not possible, and blank means passing is possible. O is the starting point,
// and W is the Final position The shortest path solution for this maze would, hence, be :
// 0,0
// 0,1
// 0,2
// 0,3
// 0,4
// 0,5
// 1,5
// 2,5
// 3,5
// 4,5
// 5,5
// The top-left corner is 0,0 and the bottom right is 5,5 in numbering.
#include<stdio.h>
int maze[6][6] = {{0,1,1,0,0,0},{0,0,0,0,1,0},{0,0,0,1,1,0},{0,0,0,1,1,0},{0,1,1,1,1,0},{0,0,0,0,0,0}};
int start[2] = {0,0},end[2]={5,5};
typedef struct n{
int i,j;
int *history;
int lenh;
} node;
typedef struct s{
node *n;
struct s *next,*prev;
}list;
node *ans = NULL;
list *stack = NULL;
list *ltop;
int found = 0;
void push(node *new);
list *move(list *start);
int isinhistory(int history[],int len,int tofind[2]);
int isfinal(node *candidate);
//void view();
int main(void){
int i;
list *lstart;
node *abc = (node*)malloc(sizeof(node));
abc->i = start[0];
abc->j = start[1];
abc->history = NULL;
abc->lenh=0;
stack = (list *)malloc(sizeof(list));
stack->next = NULL;
stack->n = abc;
lstart=stack;
ltop = stack;
while(!found) lstart = move(lstart);
if (found == 1)
for(i=0;i<(ans->lenh);i++)
printf("%d,%d\n",ans->history[2*i],ans->history[2*i+1]);
else if (found == -1)
printf("No route!");
}
// ----------- isfinal
int isfinal(node *candidate){
if(candidate->i == end[0])
if(candidate->j == end[1])
return 1;
return 0;
}
// ----------- isinhistory
int isinhistory(int history[],int len,int tofind[2]){
int i;
for(i = 0;i<len;i++)
if(tofind[0] == history[2*i] && tofind[1]==history[2*i+1]) return 1;
return 0;
}
// ---------- push
void push(node *new){
list *top = (list *)malloc(sizeof(list));
top->n=new;
top->next=ltop;
top->prev = NULL;
ltop->prev = top;
ltop=top;
}
// ---------- move
list *move(list *start){
// printf("reached move");
int left[]={(start->n->i)-1,start->n->j},right[]={(start->n->i)+1,start->n->j};
int up[]={start->n->i,(start->n->j)-1},down[] = {start->n->i,(start->n->j)+1};
int i;
node *new = (node *)malloc(sizeof(node));
// printf("1");
if((start->n->i)-1>0 && !(isinhistory(start->n->history,start->n->lenh,left))){
new->i = (start->n->i)-1;
new->j = (start->n->j);
new->history = (int *)malloc(2*(start->n->lenh+1));
new->lenh = (start->n->lenh)+1;
for(i=0;i<(start->n->lenh);i++){
new->history[2*i] = start->n->history[2*i];
new->history[2*i+1] = start->n->history[2*i+1];
}
new->history[2*i] = start->n->i;
new->history[2*i+1] = start->n->j;
if(isfinal(new)){
found = 1 ;
ans= new;
}
else push(new);
}
// printf("2");
if((start->n->i)+1<6 && !(isinhistory(start->n->history,start->n->lenh,right))){
new->i = start->n->i+1;
new->j = start->n->j;
new->history = (int *)malloc(2*(start->n->lenh+1));
new->lenh = (start->n->lenh)+1;
for(i=0;i<(start->n->lenh);i++){
new->history[2*i] = start->n->history[2*i];
new->history[2*i+1] = start->n->history[2*i+1];
}
new->history[2*i] = start->n->i;
new->history[2*i+1] = start->n->j;
if(isfinal(new)){
found=1 ;
ans= new;
}
else push(new);
}
// printf("3");
if((start->n->j)-1>0 && !(isinhistory(start->n->history,start->n->lenh,up))){
new->i = start->n->i;
new->j = start->n->j-1;
new->history = (int *)malloc(2*(start->n->lenh+1));
new->lenh = (start->n->lenh)+1;
for(i=0;i<(start->n->lenh);i++){
new->history[2*i] = start->n->history[2*i];
new->history[2*i+1] = start->n->history[2*i+1];
}
new->history[2*i] = start->n->i;
new->history[2*i+1] = start->n->j;
if(isfinal(new)){
found=1 ;
ans= new;
}
else push(new);
}
// printf("4");
if((start->n->j)+1<6 && !(isinhistory(start->n->history,start->n->lenh,down))){
new->i = start->n->i;
new->j = start->n->j+1;
new->history = (int *)malloc(2*(start->n->lenh+1));
new->lenh = (start->n->lenh)+1;
for(i=0;i<(start->n->lenh);i++){
new->history[2*i] = start->n->history[2*i];
new->history[2*i+1] = start->n->history[2*i+1];
}
new->history[2*i] = start->n->i;
new->history[2*i+1] = start->n->j;
if(isfinal(new)){
found =1 ;
ans= new;
}
else push(new);
}
if(start==ltop) found = -1;
list *temp,*top = ltop;
temp = start;
start = start->prev;
start->next = NULL;
free(temp->n->history);
free(temp);
// view();
return start;
}
// ---- view
/*void view(){
int dummy;
list *top = ltop;
while( top->next !=NULL){
printf("\n%d\n",top->n->lenh);
top=top->next;
}
scanf("%d",&dummy);
}*/
I've used a stack to keep a list of what I haven't gone through in the maze yet, and the history is used to know what i've already visited. I tried linking my stack both ways, too.

Oh.. there's the image.
HELP!