(adsbygoogle = window.adsbygoogle || []).push({ google_ad_client: "ca-pub-2960223314593660", enable_page_level_ads: true }); C program for Maze Problem - TecGlance

Header Ads

C program for Maze Problem


#include <stdio.h>
int flag=0;
void jump(int maze[][6],int visit [][6],int i,int j,int endi,int endj)
{
        if((i>=0&&i<=5)&&(j>=0&&j<=5))
        {
           if(visit[i][j]==0 && maze[i][j]==0)
           {
              visit[i][j]=1;
              if((endi==i)&&(endj==j))
                 flag=1;
              jump(maze,visit,i-1,j-1,endi,endj);
              jump(maze,visit,i-1,j,endi,endj);                                                
              jump(maze,visit,i-1,j+1,endi,endj);
              jump(maze,visit,i,j-1,endi,endj);
              jump(maze,visit,i,j+1,endi,endj);
              jump(maze,visit,i+1,j-1,endi,endj);
              jump(maze,visit,i+1,j,endi,endj);
              jump(maze,visit,i+1,j+1,endi,endj);
           }                                      
        }
}
int main()
{
   int i=0,j=0,endi,endj,visit[6][6];
   char ch;
   int maze[6][6]={{0,1,0,1,1,1},{1,0,1,1,0,0},{1,0,1,1,1,1},{1,1,0,0,1,0},{1,0,1,0,1,1},{0,0,0,1,0,0}};
   
   for(i=0;i<6;i++)
   {
      printf("\n");
      for(j=0;j<6;j++)
         printf("%d ",maze[i][j]);
   }
   do
   {
      flag=0;
      for(i=0;i<6;i++)
      {
         for(j=0;j<6;j++)
         visit[i][j]=0;
      }
      printf("\n\nEnter the starting location i: ");
      scanf("%d",&i);
      printf("\nEnter the starting location j: ");
      scanf("%d",&j);
      printf("\nEnter the end location i: ");
      scanf("%d",&endi);
      printf("\nEnter the end location j: ");
      scanf("%d",&endj);
      jump(maze,visit,i,j,endi,endj);
      if(flag==0)
         printf("\nNo path exist!");
      else
         printf("\nPath exists");
      printf("\nDo you want to continue (Y/N)?");
      fflush(stdin);
      scanf("%c",&ch);
   }while(ch!='n');
}

No comments

Powered by Blogger.