Advertisement
Guest User

Untitled

a guest
May 26th, 2015
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.49 KB | None | 0 0
  1. //This is a maze solver coded in C which will print you the solution in a text file. input is first line <no. of mazes>, second line <no. of paths>, <no. of edges>, third line is <edge #1>, <connected to edge #2>, <cost>. NO COMMAS
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. struct node{
  7.         int path1;
  8.         int path2;
  9.         int cost;
  10.         };
  11.  
  12. int neighbor( int x, int node, struct node paths[]);
  13. void cleanfgets(char input[]);
  14. int deadend(int badnodes[],int neighbors[], int node, int prevnode,int badcount);
  15. int badneighbor(int badnodes[],int neighbors[],int node, int prevnode, int badcount,int x);
  16. int getcost(int nodepaths[],struct node paths[],int totaledges);
  17.  
  18. int getcost(int nodepaths[],struct node paths[],int totaledges) //insert the path here and it gives back the cost
  19. {
  20. int x=0;
  21. int y=0;
  22. int cost=0;
  23. while( nodepaths[x] != totaledges)
  24. {
  25.     y=0;
  26.     while(1)
  27.     {
  28.     if( (nodepaths[x]==paths[y].path1 || nodepaths[x]==paths[y].path2) && (nodepaths[x+1]==paths[y].path1 || nodepaths[x+1]==paths[y].path2) )
  29.         break;
  30.     y++;
  31.     }       //find the cost of path A to B by first finding where A to B is in struct paths[]
  32.     cost+=paths[y].cost;   
  33. x++;
  34. }
  35. return cost;
  36. }
  37.  
  38.  
  39.  
  40. int badneighbor(int badnodes[],int neighbors[],int node, int prevnode, int badcount,int x)
  41. {
  42. int y=0;                //bad neighbor prevents us from hopping to a node that's already bad/done or already traversed
  43. while(y<badcount)
  44.     {
  45.         if(neighbors[x]==badnodes[y])
  46.             return 1;
  47.         y++;
  48.     }
  49.  
  50.     if (neighbors[x]==prevnode)
  51.         return 1;
  52.        
  53. return 0;
  54. }
  55.    
  56.    
  57. int deadend(int badnodes[],int neighbors[], int node, int prevnode,int badcount)
  58. {
  59.     int x=0;
  60.     int y=0;        //this function tells us if we are stuck in a corner with done nodes or traversed nodes
  61.     int goodneighbors=4;
  62.     while(x<4)
  63.     {
  64.         if(neighbors[x]==prevnode)
  65.             goodneighbors-=1;
  66.        
  67.         y=0;
  68.         while(y < badcount)
  69.         {
  70.             if(neighbors[x]==badnodes[y])       //goodneighbors is always 4, if the current neighbor is either a done node, a traversed node, or a -1 meaning an empty neighbor we subtract 1
  71.                 goodneighbors-=1;
  72.             y++;   
  73.         }
  74.         if(neighbors[x]==-1)
  75.             goodneighbors-=1;
  76.         x++;   
  77.     }
  78. if (goodneighbors==0)       //if all the neighbors are invalid then we are at a dead end
  79.     return 1;
  80. else return 0;
  81.  
  82. }
  83. int neighbor( int x, int node, struct node paths[])
  84. {
  85.         if( paths[x].path1==node || paths[x].path2==node ) 
  86.             return 1;
  87.    
  88.         else return 0;
  89. }
  90. void cleanfgets(char input[])
  91. {
  92.     int x=0;
  93.     while(input[x])
  94.     x++;
  95.     input[x-1]='\0';
  96. }
  97. int main(void)
  98. {
  99. FILE* fp = fopen("mazesolution","w"); //file name here
  100.  
  101. char input[20];
  102. int totalmazes;
  103. int totalnodes;
  104. int totaledges;
  105. int prevnode;
  106. int badcount=0;         //keeping track of how many bad neighbors
  107. int v=0;
  108. int w=0;    //for maze traversing
  109. int x=0;
  110. int y=0;    //for total mazes
  111. int z=0;
  112. int neighborcount=0;    //for counting neighbors
  113. int node;
  114. int nodecounter=0;
  115. int neighbors[4];
  116. int *badnodes;
  117. int *nodepaths;
  118. struct node *paths;
  119. memset(neighbors,-1,sizeof(neighbors));
  120. fgets(input,sizeof(input),stdin);
  121.     sscanf(input,"%d",&totalmazes);
  122.    
  123.     while(y<totalmazes)     //run the entire loop as many times as there are mazes
  124.     {
  125.         fgets(input,sizeof(input),stdin);
  126.             sscanf(input,"%d %d",&totalnodes,&totaledges);
  127.  
  128.         badnodes=malloc(sizeof(int) * totaledges);      //these initialize the badnodes, nodepath arrays depending on how large the maze is
  129.         memset(badnodes,-1,sizeof(badnodes));           //having 0 as a node means they should be initialized to some other default value to prevent confusion
  130.         paths=malloc(sizeof(struct node) * totaledges);
  131.         nodepaths=malloc(sizeof(int) * totaledges);
  132.         memset(nodepaths,-1,sizeof(nodepaths));
  133.         badcount=0;
  134.         node=0;
  135.         prevnode=-5;    //having -1 as default member of the arrays and since the maze contains 0 as a node, the default value should be something negative thats less than -1
  136.         x=0;
  137.         nodecounter=0;
  138.        
  139.         while(x<totaledges)
  140.                 {
  141.                 fgets(input,sizeof(input),stdin);
  142.                 sscanf(input,"%d %d %d", &paths[x].path1,&paths[x].path2,&paths[x].cost);
  143.                 x++;
  144.                 }           //store the maze in paths[]
  145.         x=0;
  146.  
  147.                
  148.         while(node!=totaledges) //start of maze solving
  149.             {      
  150.            
  151.             memset(nodepaths,-1,sizeof(nodepaths));
  152.             node=0;
  153.             prevnode=-5;
  154.             w=0;
  155.             while(w<totaledges)     //node travelling loop
  156.                 {
  157.                
  158.                 nodepaths[w]=node;      //nodepaths takes note of the path taken so that if we find the solution we just print this out
  159.                     neighborcount=0;
  160.                     memset(neighbors,-1,sizeof(neighbors));
  161.                     x=0;
  162.                     while(x<totaledges)             //find neighbors and store into neighbor array
  163.                             {
  164.                             if(neighbor(x,node,paths)==1){
  165.                                 if(node==paths[x].path1)
  166.                                 neighbors[neighborcount]=paths[x].path2;
  167.                                
  168.                                 else
  169.                                 neighbors[neighborcount]=paths[x].path1;
  170.                                
  171.                                 neighborcount++;
  172.                             }
  173.                          
  174.                             x++;
  175.                             }
  176.                    
  177.                     if(node==totaledges)    {  
  178.                         break;  }       //found solution if this happens and it should break out of the bigger loop too
  179.                     if(deadend(badnodes,neighbors,node,prevnode,badcount)==1) {
  180.                        
  181.                         badnodes[badcount]=node;        //get out if theres no neighbors left to check
  182.                         badcount++;                     // bad/done nodes are nodes that are checked already
  183.                         node=0; prevnode=-5;
  184.                         break;
  185.                         }
  186.                                
  187.                
  188.                            
  189.                         x=0;
  190.                         while(badneighbor(badnodes, neighbors, node, prevnode,badcount,x)==1)   //node travelling
  191.                             {
  192.                             x++;
  193.                             }
  194.                         prevnode=node;          //reassign the current working node
  195.                         node=neighbors[x];
  196.                                     neighborcount=0;
  197.                     memset(neighbors,-1,sizeof(neighbors));
  198.                     x=0;
  199.                     while(x<totaledges)             //find neighbors and store into neighbor array
  200.                             {
  201.                             if(neighbor(x,node,paths)==1){
  202.                                 if(node==paths[x].path1)
  203.                                 neighbors[neighborcount]=paths[x].path2;
  204.                                
  205.                                 else
  206.                                 neighbors[neighborcount]=paths[x].path1;
  207.                                
  208.                                 neighborcount++;
  209.                             }
  210.                          
  211.                             x++;
  212.                             }
  213.                            
  214.                
  215.                     w++;
  216.                 }
  217.                 if(node==totaledges)
  218.                     break;
  219.                 z++;
  220.             }
  221.             x=0;
  222.             fprintf(fp,"Maze #%d\n",(y+1));
  223.             fprintf(fp,"Cost: %d \n",getcost(nodepaths,paths,totaledges));
  224.             while(nodepaths[x]<totaledges)          //nodepaths contains the solution
  225.             {
  226.             fprintf(fp,"%d ",nodepaths[x]);
  227.             x++;
  228.             }
  229.             fprintf(fp,"%d\n",nodepaths[x]);
  230.            
  231.             fprintf(fp,"%d Nodes traversed\n\n", (x+1) );
  232.                 free(badnodes);     //delete arrays, re-running loop will initialize new ones with proper allocatoins using malloc
  233.                 free(nodepaths);
  234.                 free(paths);
  235.         y++;
  236.     }                   fclose(fp);
  237.  
  238. return 0;
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement