Print all possible paths from top left to bottom right of a mXn matrix
The problem is to print all the possible paths from top left to bottom right of a mXn matrix with the constraints that from each cell you can either move only to right or down.
To find all the paths from (k,l) to (m,n), use recursion.
find all the paths from (k, l+1) to (m,n); add (k,l) to the front of each one.
find all the paths from (k+1, l) to (m,n); add (k,l) to the front of each one.
the paths from (m,n) to (m,n) is [].
Add handling of the boundaries.
You do not need to store a matrix in any form.
Related
A short introduction:
I have a body that is expressed by the outer-surface, given in STL form (set of triangular elements and their outside-pointing normal vector).
I'm trying to detect if a point given by coordinates is inside or outside the body.
The problem:
How do I find the nearest element to a given point?
More specifically, say you have found the nearest vertex to the point and this vertex is shared by two (or more) elements. Which is is the "nearest" one?
Note that the end result is determining if the point is inside or outside the body. A simple normal distance (dot product with the normal) does not solve the problem and can lead to ambiguous result based on which of the elements, sharing the node is selected.
Using the centroid of the element is also problematic.
Any suggestions, ideas (especially from people who have been involved in this issue before) are most welcomed!
EDIT:
I'll make the issue slightly harder. Say there's an open surface (but it covers the whole domain so that every point is on one of two sides of the surface, either in or out, based on the direction relative to the normal.
This also needs to be answered using the same approach.
EDIT2:
Answer was found!
Hope this helps!
The problem was answered with a variation of the ray-intersection method. 1. Find the nearest vertex, using the L2 norm (squared). 2. Consider the elements which share the vertex. It is recommended to have a connectivity list and not map them every time. 3. Cast a ray is cast from the interest point to the centroid of the first element. 4. Check among the elements in <2> which intersect the ray and select the one with the shortest intersection distance 5. Use the dot product of the intersection vector with the element normal (negative sign = outside, else inside)
(This was added to the question post itself)
Let me first define my problem,
I am working on an indoor navigation problem. So I constructed a graph to simulate possible paths. I can easily calculate the shortest path with Dijkstra and draw it on a map. So far, so good.
But this is not enough,
I need to give instruction to user to navigate him.
For example:
"Turn Right"
"Turn Left"
"Go on from the left"
To give these kind of instructions I need to know which path is on the left and which path is on the right.
And here is what I have to solve this:
1. A undirected weighted graph
2. The shortest path which contains vertices and edges
3. X and Y Coordinates of each vertices
By the way I will do this in .Net by using beacon technology.
Do you know how to separate left and right edges so I can give direction messages to user?
Thanks.
The easiest way I can think of is to take the cross product of the vector representing the direction the player is facing/traveling and the vector representing direction you want the player to go in. Whether the player must turn left or right depends on whether the result's Y-coordinate is positive or negative, but which is which depends on the handedness of the coordinate system. I would just pick one and try it. You have a 50% of being right, and it's easy to reverse if you're wrong.
Edit:
Here we see that a×b points up when a is to the right of b. However, we also see that -a×b points down. So, if a were pointing in the opposite direction—to the left—then the cross product would point down.
The dot product approach does not work in two dimensions. For this case you want to use the sign of the determinant of the matrix [A B], where A and B are your column vectors. A pseudo-code would be
c=sign(det([A B]))
Here, if c>0 is means that B is to the left. This will switch depending on the order of A and B in your matrix.
In a given 2D square (n*n) even size array, i want to traverse from a starting corner till its center. Below is the image for more info.
My algorithm is to start from corner and maintain two global variable as currentX and currentY and run a loop until currentX and currentY reach to the center. Below is my pseudo code-
x=0
y=0
currentX=0
currentY=0
while(currentX != centerX and currentY != centerY){
currentX=travel_in_x_plus_direction(x,n);
currenty=travel_in_y_plus_direction(y,n);
currentX=travel_in_x_minux_direction(currentX,x);
currentY=travel_in_y_minux_direction(currentY,y-1);
n--;
x--;
y--;
}
The function travel_in_x_plus_direction(currentX) traverse the array starting from currentX till x and returns the final value of x. The same concept applies for rest of the functions also.
Is this the right way? Is there any better way to traverse it in the same manner?
Psuedocode "uses the structural conventions of a programming language, but is intended for human reading rather than machine reading." (http://en.wikipedia.org/wiki/Pseudocode)
I would suggest that writing psuedocode that conforms to this definition would be of great benefit to you and help you think about how you are going about solving your problem.
Your algorithm
Seems to suggest that you are
checking if you are at your goal "END", and if you are not
Move right
Move down
Move left
Move up
which means you will never get anywhere.
Starting point for a solution
My container is of size n*n
therefore intially the boundary is n*n
if I travel through an individual square, it becomes part of the boundary.
The path is quite simple, begin by moving right, and then whenever blocked change direction. The sequence of directions is right, down, left, up in that order, until the goal is reached.
HorizontalMax = n (the right wall)
HorizontalMin = 0 (the left wall)
VerticalMax = n (the bottom wall)
VerticalMin = 0 (the top wall)
While not at goal
While not at right boundary or goal
Move right
Increment VerticalMin (you just moved along the top wall)
While not at bottom boundary or goal
Move down
Decrement HorizontalMax (you just moved along the right wall)
While not at left boundary or goal
Move left
Decrement VerticalMax (you just moved along the bottom wall)
While not at top boundary or goal
Move up
Increment HorizontalMin (you just moved along the left wall)
End
I want to write an algorithm using a dynamic programming technique, which does the following:
Find number of monotonic paths along the edges of a grid with n × n square cells, which do not pass above the diagonal. A monotonic path is one which starts in the lower left corner, finishes in the upper right corner, and consists entirely of edges pointing rightwards or upwards.
I had some ideas, but can't figure out how to do it right.
First, find a base for your recursion by solving a degenerate case (a 0 x 0 grid). Then look for a recurrence step by imagining that part of the problem, say, K x M is already solved, and see if you can expand upon that solution by adding one row or one column to it, making the solution K+1 x M or K x M+1. This should be simple: for each point you add, see if a grid point is below the diagonal, and then add up the number of paths leading to that point from the bottom and from the left. One of these points would be in the K x M, the other would be in the additional row or column that you are building.
With the degenerate case and a recursive step in hand, build your solution by first solving a 0 x N problem, then 1 x N, then 2 x N and so on, until you have your N x N solution.
Here is a possible recursion that considers only square grids.
There are two kinds of monotone paths over the n×n grid that do not cross the diagonal: those that touch the diagonal at some intermediate point (i,i) with 0 < i < n, and those that don't.
A path over the n×n grid that first touches the diagonal at (i,i) can be split in two: one path over the i×i grid that does not touch the diagonal, and another over the (n-i)×(n-i) grid and thay may touch the diagonal. This suggests that you can count those with a recursion that considers all possible i.
A path that does not touch the diagonal will start with "right", and end with "up". Between these two moves is a monotone path over a (n-1)×(n-1) grid that does not cross the diagonal but may touch it.
What we are computing here is the nth Catalan number. There is a formula for it if you want to verify your recursive computation.
Let number of path reaching coordinate (i,j) be P(i,j). Therefore (assuming bottom left corner is (0,0)):
P(i,j) = P(i-1,j) + P(i,j-1)
You can further put conditions of coordinate not going below the diagonal. That is: i ranges from 0..n, j ranges from 0..n, but i<=j always.
I want to generate a cube where each face is divided into bits, like the following image:
http://img59.imageshack.us/img59/2504/gridcube165c3.jpg
Now, I can do this pretty simply if I'm just rendering quads, by just spacing vertices along each face plane at regular intervals, but my problem comes in when I want to turn the whole thing into a triangle strip. I've just got no idea how to unwrap it programmatically- is there some pattern to unwrapping that I'd follow?
I'm thinking of starting with the vertex at the top left corner as Row 0 Column 0 (R0C0), I'd want (first triangle) R0C0, ROC1, R1C1, (second triangle) R0C0, R1C0, R1C1 and so forth, and then when I reach the end of a row I guess I'd use a degenerate triangle to move to the next row, and then when I reach the end of the face I'd do the same to start a new face.
My main problem is that I can't visualize the program loop that would do this. I can reason out which vertex comes next visually, which is how I worked out the order above, but when I try to think programmatically I just stare blankly.
Even worse, with the end product I want the generated cube to be UV-mapped with a simple cube-map unwrap (the kind that looks like a T or t).
I guess, really, the best solution would be to find a library that already does this for me.
You could take a look at Ignacio Castaño's 'Optimal Grid Rendering' even though it's not triangle strips, it may inspire you.
Otherwise, you could use NVTriStrip library and be done with it.