i started out on programming d maze problem..and this is where i have reached
i am having problems in certain areas wich i have mentioned in d code..plz have a look..
Code:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct str
{
int x1,y1;
struct str *next,*prev;
};
struct pos
{
int i,j;
};
void insert(struct str **,int,int);
struct pos delet(struct str **);
void main()
{
int i=0,j=0,k,val=0;
int x2,y2;
int move;
static int r;
struct pos position;
int **maze,x,y;
struct str *start,*temp;
clrscr();
printf("Enter the order of matrix:\n");
scanf("%d",&y);
scanf("%d",&x);
maze=(int **)calloc(x,sizeof(int *));
for(i=0;i<x;i++)
{
maze[i]=(int *)calloc(y,sizeof(int));
}
printf("\nEnter location of\n 1)passages(0)\n 2)blockages(1)\n");
for(i=0;i<x;i++)
{
for(j=0;j<y;j++) //this portion does not link
{ //with d rest of program
printf("\nmaze[%d][%d]=",i,j);
scanf("%d",&val);
}
}
clrscr();
move=0;
r=0;
maze[0][0]=2; //Starting position of rat
maze[x-1][y-1]=3; //Location of food
x2=0; y2=0; //temporary coordinates
/*i have tried to define all the possible combinations below to be worked out*/
if(maze[0][1]==3||maze[0][1]==0)
{
move++;
}
else if(maze[1][0]==3||maze[1][0]==0)
{
move++;
}
start=temp=NULL;
do
{
clrscr();
for(i=0;i<x;i++)
{
printf("\n\n");
for(j=0;j<y;j++)
{
if(maze[i][j]==2)
{
textcolor(RED+BLINK); //some beautifications
cprintf(" %d",maze[i][j]);
}
else
{
textcolor(WHITE);
printf("\t%d",maze[i][j]);
}
}
}
if( ((maze[x2][y2+1]==0)||(maze[x2][y2+1]==3)) && ( ((y2+1)<y) && ((y2+1)>0)) )
{
if(maze[x2][y2+1]==3)
{
printf("\n\nfood is found");
getch();
exit(0);
}
maze[x2][y2]=4;
insert(&start,x2,y2);
y2=y2+1;
maze[x2][y2]=2;
}
else if( ((maze[x2-1][y2]==0)||(maze[x2-1][y2]==3)) && ( ((x2-1)<x) && ((x2-1)>=0)) )
{
if(maze[x2-1][y2]==3)
{
printf("\n\nfood is found");
getch();
exit(0);
}
maze[x2][y2]=4;
insert(&start,x2,y2);
x2=x2-1;
maze[x2][y2]=2;
}
else if( ((maze[x2][y2-1]==0)||(maze[x2][y2-1]==3)) && ( ((y2-1)<y) && ((y2-1)>=0)) )
{
if(maze[x2][y2-1]==3)
{
printf("\n\nfood is found");
getch();
exit(0);
}
maze[x2][y2]=4;
insert(&start,x2,y2);
y2=y2-1;
maze[x2][y2]=2;
}
else if( ((maze[x2+1][y2]==0)||(maze[x2+1][y2]==3)) && ( ((x2+1)<x) && ((x2+1)>0)) )
{
if(maze[x2+1][y2]==3)
{
printf("\n\nfood is found");
getch();
exit(0);
}
maze[x2][y2]=4;
insert(&start,x2,y2);
x2=x2+1;
maze[x2][y2]=2;
}
else
{
maze[x2][y2]=5;
position=delet(&start);
x2=position.i;
y2=position.j;
maze[x2][y2]=2;
}
if(x2==0&&y2==0)
{
r++;
}
if(r>move)
{
printf("\n\nnot found ");
getch();
exit(0);
}
getch();
}while(maze[x2][y2]!=3);
}
void insert(struct str **start,int x2,int y2)
{
struct str *node,*temp;
node=temp=NULL;
if(*start==NULL)
{
node=(struct str *)malloc(sizeof(struct str));
*start=node;
node->prev=NULL;
node->next=NULL;
node->x1=x2;
node->y1=y2;
}
else
{
node=(struct str *)malloc(sizeof(struct str));
for(temp=*start;temp->next!=NULL;temp=temp->next)
{}
temp->next=node;
node->next=NULL;
node->prev=temp;
node->x1=x2;
node->y1=y2;
}
}
struct pos delet(struct str **start)
{
struct pos p;
struct str *temp;
temp=NULL;
for(temp=*start;temp->next!=NULL;temp=temp->next)
{}
p.i=temp->x1;
p.j=temp->y1;
temp->prev->next=NULL;
return(p);
}
The problem i am now facing is that i am not able to link the maze(wich i am entering from d user of 0's and 1's) with processing part due to wich the maze displayed contains all 0's...plz help!!