move robot

// move on an N*N matrix, can only left and down
// how many paths. using recursive
// X - current x-coordinate
// Y - current y-coordinate
// v - vector to hold path
public void robotMove( int N, int X, int Y, Vector v )
{
    Vector<Point> path = new Vector<Point>( );
    path = v.clone();   

    if( N <= 1 )
    {
        return;
    }
    // already arrived the lower right corner
    if( X == N-1 && Y == N-1 )
    {
        // recursive would be terminated

        path.add( new Point(X,Y));
        System.out.println( path.toString() );
        return;
    }
    else if( X < N-1 && Y < N-1 )
    {
        Point p = new Point(X, Y+1 );
        path.add( p );
        robotMove( a, N, X, Y+1, path );
        // very important, after you come out from recursive all
        // you need to remove the current point to preare for the next
        // recursive, to restore to initial status
        path.remove( p );

        p = new Point(X+1, Y );
        path.add( p );
        robotMove( a, N, X+1, Y, path );
        path.remove( p ); // using LinkedList may be better for performance
    }
    else if( X == N-1 )
    {
        path.add( new Point(X,Y+1) );
        robotMove( a, N, X, Y+1, path );
    }
    else if( Y == N-1 )
    {
        path.add( new Point(X+1,Y) );
        robotMove( a, N, X+1, Y, path );
    }
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s