maze in c++

Discussion in 'C++' started by seacat, Jun 29, 2008.

  1. seacat

    seacat New Member

    Joined:
    Jun 29, 2008
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    i have written this code ..if i use for loop it writes all zeros to 2 and if i use while then it dont display result i am lost ...
    Code:
    #include <cstdlib>
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
           
           //----------------------------------------------------Print maze
        void fnPrintMaze(int m,int n,int**a)
           {
             for (int i=0; i<m; i++)
              {
              for (int j=0; j<n; j++)
                  {
                   cout<<a[i][j];
                   cout<<" ";
                   }
              cout<<endl;  
              }    
           }
           
          //----------------------------------------------------Find Path
    
         void fnFindPath(int m,int n, int **a)
         {
             int i=0;
             int j=0;
           
              
             //if(i==0 && j==0)
              a[0][0]=2;
              
           do{   
              
              if((i>=0 && j+1>=0 && i<=m-1 && j+1<=n-1 )&& a[i][j+1]==0)//right
              {j++;
                      a[i][j]=2;
              }
              
             else if((i+1>=0 && j>=0 && i+1<=m-1 && j<=n-1) && a[i+1][j]==0)//down
              {i++;
              a[i][j]=2;
              
              }
              
              
               else if((i>=0 && j-1>=0 && i<=m-1 && j-1<=n-1) && a[i][j-1]==0)    //left  
              {
              j--;
              a[i][i]=2;
              }
              
              
              
              else if((i-1>=0 && j>=0 && i-1<=m-1 && j<n-1) && a[i-1][j]==0) //up
              {
              i--;
              a[i][j]=2;
              } 
           else 
            {
            if(i>=0 && j+1>0 && i<=m-1 &&j+1<=n-1 && a[i][j+1]==2)
              {
                      a[i][j]=3;
                      j++;
              }
              
              else if(i+1>0 && j>=0 && i+1<=m-1 &&j<=n-1 && a[i+1][j]==2)
              {
              a[i][j]=3;
              i++;
              }
             
              
               else if(i>=0 && j-1>=0 && i<=m-1 &&j-1<=n-1 && a[i][j-1]==2)     
              {
    
              a[i][i]=3;
              j--;
              }
              
              
              
              else if(i-1>=0 && j>=0 && i-1<=m-1 &&j<n-1 && a[i-1][j]==2)
              {
              
              a[i][j]=3;
              i--;
              
              }
            
            
            
            
            }
            }while( !( i==m-1 && j==n-1));
              
         
         }
          
    
    
    
    int main(int argc, char *argv[])
    {
        int m, n;
        int **maze;
        
        fstream fin("maze.txt", ios::in);
        fin>>m;
        fin>>n;
            
        maze= new int*[m];                                    //defining rows
              
        for (int i=0; i<=m-1; i++)
              maze[i]=new int[n];                            //defining columns
        
        
        for (int i=0; i<=m-1; i++)                              //reading values from txt and putting in matrix
              for (int j=0; j<=n-1; j++)
                  fin>>maze[i][j];
                  
           
    
         fnPrintMaze(m,n,maze);
           
        //for (int i=0; i<=m-1; i++)
            // for (int j=0; j<=n-1; j++)
          
                   fnFindPath( m,n, maze);      
    
    
        fnPrintMaze(m,n,maze);
        
      
        system("PAUSE");
        return EXIT_SUCCESS;
    }
     
    Last edited by a moderator: Jun 29, 2008

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice