CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: pattern in code

Threaded View

  1. #1
    Join Date
    Apr 2017
    Posts
    2

    pattern in code

    hello i'm a newbie, i am learning c++ and working with my teacher. Recently i got stuck with a problem. i am working on a code for shortest path.
    i have data for obstacle and i need to input them in my code. this is the code:

    Code:
    int main()
    {
        int i,j,x,y,x1,y1;
    
        for(i=0; i < 12; ++i)
            for(j=0; j < 12; ++j)
               RES[i][j]=0;  // way
    
        RES[1][5]=3;                         // start
        RES[10][5]=5;                        // goal
        RES[3][4]=RES[3][5]=RES[3][6]=2;     // block
        x=1;
        y=5;
    
    
        for(i=0; i < 12; ++i)
            for(j=0; j < 12; ++j)
               R[i][j]=254;  // walkable
    
        R[1][5]=253;                         // start
        R[10][5]=0;                          // finish
    
        R[3][4]=R[3][5]=R[3][6]=255;         // pathless, bunker
    
        for(j=0; j < 12; ++j) 
            R[0][j] =255;
    
        for(j=0; j < 12; ++j)
             R[11][j]=255;
        
        for(i=0; i < 12; ++i)
            R[i][0] =255;
        
        for(i=0; i < 12; ++i)
            R[i][11]=255;
    
        ofstream RRR;
        RRR.open("OutWalk.txt", ios::out | ios::app);
        if(!RRR)
            RRR.open("OutWalk.txt", ios::out);
    
    
        for(N=0; N < Nmax; ++N)
        {
            cout<<"  N = " << N << endl;
            for(i=1; i < 11; ++i)
                for(j=1; j < 11; ++j)
                {
                    if(R[i][j] == N)
                    {
                        if(R[i+1][j] == 253)
                        {
                            x=i+1;    // start
                            y=j;
                            goto PATH;
                        }
                        if(R[i+1][j] == 254) 
                           R[i+1][j]=N+1;
                        
                        if(R[i-1][j] == 253)
                        {
                            x=i-1;    // start
                            y=j;
                            goto PATH;
                        }
    
                        if(R[i-1][j] == 254)
                             R[i-1][j]=N+1;
                        
                        if(R[i][j+1] == 253)
                        {
                            x=i;    // start
                            y=j+1;
                            goto PATH;
                        }
    
                        if(R[i][j+1] == 254)
                             R[i][j+1]=N+1;
                        
                        if(R[i][j-1] == 253)
                        {
                            x=i;
                            y=j-1;
                            goto PATH;
                        }
    
                        if(R[i][j-1] == 254) 
                              R[i][j-1]=N+1;            // start
    
                    }
    
                } // R[12][12]
        }    // N
    
    PATH:
        if(R[x+1][y] > R[x-1][y])
        {
            x1=x-1;
            y1=y;
        }
        else
        {
            x1=x+1;
            y1=y;
        }
    
        if(R[x1][y1] > R[x][y+1])
        {
            x1=x;
            y1=y+1;
        }
    
        if(R[x1][y1] > R[x][y-1])
        {
            x1=x;
            y1=y-1;
        }
    
        RES[x1][y1]=1;
        if(R[x1][y1] == 0)
        {
            RES[x1][y1]=5;
            goto END;
        }
    
        x=x1;
        y=y1;
        goto PATH;
    
    END:
        cout << "  N = " << N << endl;
        RRR << endl;
    
        RRR << endl;
        RRR << " Number of circuit = " << N << "  Goal: x = "<< x1 << " y = "<< y1 << endl;
        for(i=10; i > 0; i--)
        {
            RRR << endl;
            for(j=1; j < 11; ++j)
                RRR << R[i][j] << " \t ";
        }
    
        RRR << endl;
        for(i=10; i > 0; i--)
        {
            RRR << endl;
            for(j=1; j < 11; ++j)
                RRR << RES[i][j] << "  ";
        }
    
        return 0;
    }
    and i have a data.txt file containg values
    0 9
    1 7
    2 5
    3 2
    4 0

    with them i have to make pattern like this

    0 0 0 0 0 0 0 0 0
    0 2 2 2 2 2 2 2 0
    0 2 5 5 5 5 5 2 0
    0 2 5 7 7 7 5 2 0
    0 2 5 7 9 7 5 2 0
    0 2 5 7 7 7 5 2 0
    0 2 5 5 5 5 5 2 0
    0 2 2 2 2 2 2 2 0
    0 0 0 0 0 0 0 0 0

    can anyone help me with it?
    Last edited by 2kaud; April 10th, 2017 at 11:01 AM. Reason: Added code tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured