Code:
#include <stdlib.h>
#include <stdio.h>
struct snode *check2(struct snode *ptr,int rowc2,int colc2);
void addToO(struct snode *i1,int rowo,int colo,int co);
void remO();
void adj(struct snode *i1);
int bscore(int rowb,int colb);
int check(int rowc,int colc);
int addToC(struct snode *i1);
void create(int i,int j);
void path();
void pathnode();
struct node
{
int row,col;
struct node *p,*n;
struct colm *dl;
};
struct colm
{
int col;
struct colm *d;
};
struct snode
{
int row,col,a,b,c;
struct snode *p,*n,*f;
};
struct node *start=NULL,*end=NULL,*head=NULL,*tail=NULL;
struct colm *temp2=NULL;
struct snode *openh=NULL,*closeh=NULL,*tempg=NULL,*tempg1=NULL;
int maxr,nop=0;
int main()
{
int i=1,j=1,ps=0;
char c;
FILE *f=NULL;
f=fopen("map.txt","r");
while((c=fgetc(f))!=EOF)
{
if(c==10)
{
i++;
j=0;
}
else if(c=='E' || c=='e')
{
start=(struct node *)malloc(sizeof(struct node));
start->row=i;
start->col=j;
}
else if(c=='X' || c=='x')
{
end=(struct node *)malloc(sizeof(struct node));
end->row=i;
end->col=j;
}
else if(c=='#')
{
create(i,j);
}
j++;
}
maxr=i;
fclose(f);
if(start==NULL || end==NULL)
{
printf("Wrong Map");
exit(0);
}
path();
if(nop)
printf("nopath");
else
{
pathnode();
f=fopen("map.txt","r");
i=1;
j=1;
while((c=fgetc(f))!=EOF)
{
if(check2(tempg1,i,j++))
{
ps++;
printf("P");
}
else
printf("%c",c);
if(c==10)
{
i++;
j=1;
}
}
}
fclose(f);
printf("\n\nNumber of P: %d",ps);
}
void create(int i,int j)
{
struct node *tempn=NULL;
struct colm *tempc=NULL;
if(head==NULL)
{
tempn=(struct node *)malloc(sizeof(struct node));
tempn->row=i;
tempn->col=j;
tempn->p=NULL;
tempn->n=NULL;
tempn->dl=NULL;
head=tempn;
tail=tempn;
}
else
{
if(i==tail->row)
{
tempc=(struct colm *)malloc(sizeof(struct colm));
tempc->col=j;
tempc->d=NULL;
if(tail->dl==NULL)
{
tail->dl=tempc;
temp2=tempc;
}
else
{
temp2->d=tempc;
temp2=tempc;
}
}
else
{
tempn=(struct node *)malloc(sizeof(struct node));
tempn->row=i;
tempn->col=j;
tempn->n=tail->n;
tail->n=tempn;
tempn->p=tail;
tempn->dl=NULL;
tail=tempn;
}
}
}
int check(int rowc,int colc)
{
struct node *t1=NULL;
struct colm *t2=NULL;
t1=head;
while(t1)
{
if(t1->row==rowc)
{
if(t1->col==colc)
return 1;
else if(t1->dl)
{
t2=t1->dl;
while(t2)
{
if(t2->col==colc)
return 1;
else
t2=t2->d;
}
}
return 0;
}
else
t1=t1->n;
}
return 0;
}
struct snode *check2(struct snode *ptr,int rowc2,int colc2)
{
struct snode *temp=NULL;
temp=ptr;
while(temp)
{
if(temp->row==rowc2 && temp->col==colc2)
{
tempg=temp;
return tempg;
}
temp=temp->n;
}
return NULL;
}
void path()
{
int brk;
struct snode *i1=NULL;
i1=(struct snode *)malloc(sizeof(struct snode));
i1->row=start->row;
i1->col=start->col;
i1->a=0;
i1->b=bscore(i1->row,i1->col);
i1->c=i1->b;
i1->f=NULL;
i1->p=NULL;
i1->n=NULL;
openh=i1;
while(1)
{
brk=addToC(i1);
if(!brk)
break;
remO();
adj(closeh);
i1=openh;
if(i1==NULL)
{
nop=1;
break;
}
}
}
int addToC(struct snode *i1)
{
struct snode *i2=NULL;
i2=(struct snode *)malloc(sizeof(struct snode));
i2->row=i1->row;
i2->col=i1->col;
i2->a=i1->a;
i2->b=i1->b;
i2->c=i1->c;
i2->f=i1->f;
if(closeh)
i2->n=closeh;
else
i2->n=NULL;
i2->p=NULL;
closeh=i2;
if(i1->row==end->row && i1->col==end->col)
return 0;
else return 1;
}
void remO()
{
struct snode *i2=NULL;
i2=openh;
openh=openh->n;
openh->p=NULL;
free(i2);
}
void adj(struct snode *i1)
{
int c9,j1,j2;
for(j1=-1;j1<=1;j1++)
for(j2=-1;j2<=1;j2++)
if((!(j1==0 && j2==0)) && (i1->row+j1>0) && (i1->col+j2>0) && (i1->row+j1<=maxr) && (!check(i1->row+j1,i1->col+j2)) && (!check2(closeh,i1->row+j1,i1->col+j2)))
{
c9=i1->a+1+bscore(i1->row+j1,i1->col+j2);
addToO(i1,i1->row+j1,i1->col+j2,c9);
}
}
void addToO(struct snode *i1,int rowo,int colo,int co)
{
struct snode *i2=NULL,*temp=NULL;
if(check2(openh,rowo,colo))
{
i2=check2(openh,rowo,colo);
if(i1->a+1<i2->a)
{
i2->a=i1->a+1;
i2->c=i2->a+i2->b;
i2->f=i1;
temp=i2->p;
while(i2->c<temp->c && temp)
temp=temp->p;
i2->p->n=i2->n;
i2->n->p=i2->p;
i2->p=temp;
i2->n=temp->n;
i2->n->p=i2;
if(temp)
temp->n=i2;
else
openh=i2;
}
}
else
{
i2=(struct snode *)malloc(sizeof(struct snode));
i2->row=rowo;
i2->col=colo;
i2->a=i1->a+1;
i2->b=co-i2->a;
i2->c=co;
i2->f=i1;
i2->n=NULL;
i2->p=NULL;
if(openh==NULL)
openh=i2;
else
{
temp=openh;
while(i2->c>temp->c && temp->n)
temp=temp->n;
if(i2->c>temp->c)
{
temp->n=i2;
i2->p=temp;
i2->n=NULL;
}
else
{
if(temp==openh)
openh=i2;
i2->n=temp;
i2->p=temp->p;
temp->p->n=i2;
temp->p=i2;
}
}
}
}
int bscore(int rowb,int colb)
{
int x,y;
x=rowb-end->row;
y=colb-end->col;
x=x<0?-x:x;
y=y<0?-y:y;
return x+y;
}
void pathnode()
{
struct snode *temp=NULL,*temp1=NULL;
temp=check2(closeh,end->row,end->col)->f;
while(temp->n)
{
temp1=(struct snode *)malloc(sizeof(struct snode));
temp1->row=temp->row;
temp1->col=temp->col;
temp1->n=tempg1;
tempg1=temp1;
temp=temp->f;
}
}