Unique paths Dynamic Programming - recursion

You have a 2D grid. You have a limited number of moves. You can go right, down, left, and up. And you need to find the number of ways to escape the grid. So a 1 x 2 grid with 3 moves has 9 unique paths to escape the grid (escaping is going out of bounds).
I need to come up with a dynamic programming approach and so far I can only think about the different moves you can have:
You are on the grid and not out of moves: keep going
You are out of bounds and out of moves: 1 + try to find another path
You are out of bounds and not out of moves: 1 + go back a step and find another path
You are on the gird and out of moves: this path does not count, go back a step and find another path
I can't think of a way of storing the values to avoid recalculation since you can start at any point and there are multiple places to finish from, as well as having a limited number of moves.

Related

Uber Question : Toggle a cell from 0 to 1 to get the optimum path

In a Binary maze with 0 and 1, 0 is the valid cell to which we can travel and 1 means that the cell is blocked. Given source and destination. We have to find-
1. IF path exists, if yes, find shortest path.
2. If we are given a chance to toggle single cell from 1 to 0 , which cell you will toggle so that you will surely get the shortest path.
For the second part how to check for each of the cell without toggling it one by one, if there any efficient way to do the same?
I think instead of using a dynamic programing approach where you at any index i of the grid take the min of the all possible direction you can take, you can use a greedy approach where you create the graph in such a way that a index i is connected to all the indexes around it(max possible 8) in the grid which has zero value and use the shortest path algorithm between given two nodes. This will make toggling also easy you only toggle the set of indexes which are shared between the source and the destination.

I need to create a "map" of 0s and 1s

I know this is not much of a question but a seek of help, but I wanted to give it a shot too since I'm really blocked with this.
I want to create a sort of map (representend by a string) that is filled with numbers in a way similar to this example:
000000000000000000
001111000000011000
011111100000111110
000111110001111100
000011000000011000
000000000000000000
So the idea is that 0 would be like "nothing" or imagine this is a world map and 0s are grass tiles while 1s could be for example ground tiles.
I want to be able to generate something like that (on a bigger scale) with lots of small and big patches of "ground". But obviously it can't just be random 1s and 0s everywhere as it wouldn't look natural, it has to be somewhat natural looking shapes.
My maths are just too bad to think of a way to achieve this out of my head,
I'm not asking for the code, just seeking some help.
If you guys could point me in the right direction it would help a lot :)
Do you just want to generate this for fun, you might be looking for ascii art generator. There are lot of online tools available, just a search away for the phrase ascii art generator.
Background: Every image is made up of pixels. Every one of these pixels have a color defined by (R,G,B) with each component ranging from 0-255.
Defined Problem: You want to convert each pixel of an image to 0 or 1 based on their color. Color would be the defining attribute here as we expect the 0 attribute and 1 attribute to be visually different.
Solution: You'll need to create a function which will map each pixel of an image based on their RGB values to either 0 or 1. You'll have to classify ranges for them according to which every pixel will either be converted to a 0 or 1 as per their weight.
Implementation: I'll probably be looking towards Python Libraries. There are tons of them and I'm certain there must be one to access an image at a bit level. When you have the number of pixels in length and width, use those parameters to construct an array of that size and loop your function over the image pixels and go on filling the results in the array.
Crude Way: You can alternatively visit this website which will do it for you. Just in case you want it done at the easiest way possible: https://www.text-image.com/convert/
There are many more like this if you search for them.
If you want an algorithm you could probably start with all 0s. Then randomly and sparsely place some 1s into the field. Now take each of the placed 1s and randomly select a direction (up, down, left, right) and a number n of steps between 0 and a small number m, e.g. 3. Let the selected 1 "grow" in that direction n times. i.e. cover the n spots in that direction with 1s. Repeat for any newly placed 1s (maybe omit those which were already placed over previously existing 1s) until no newly placed 1 is unprocessed. That should at least give you some "connected patches", maybe similar to plants e.g. grass might spread. And maybe play with m a little, to vary the size of the "patches".
I found what I was looking for,
I'll do it with Perlin Noise :)

Improving shortest route algorithm

I have working A* algorithm, but I want to improve it. It finds the shortest path and everything appears as it should. The problem is that I want for route to keep distance (1 empty node or more) from non-walkable grid cells.
Any ideas how to solve this problem?
How it looks now:
What I want to do:
Solution 1:
Make your dot think it is bigger than it actually is (3x3 instead of 1x1) then work out path based on edges of the 3x3 square. (May require collision detection code, also a bypass to allow it through 1 block size gaps.)
Solution 2:
Calculate path as normal with an additional check so move your block 1 space away from the wall if there is space to do so.

Generate subdivided triangle-strip cube?

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.

Formula to determine if a line segment intersects a circle (flat)

Apologies if this is considered a repeat question, but the answers I've seen on here are too complex for my needs.
I simply need to find out if a line segment intersects a circle. I don't need to find the distance to the line from the circle center, I don't need to solve for the points of intersection.
The reason I need something simple is that I have to code this in SQL and am unable to call out to external libraries, and need to write this formula in a WHERE clause... basicaly it has to be done in a single statement that I can plug values in to.
Assuming 2 points A (Ax,Ay) and B (Bx,By) to describe the line segment, and a circle with center point C (Cx,Cy) and radius R, the formula I am currently using is:
( RR ( (Ax-Bx)(Ax-Bx) + (Ay-By)(Ay-By) ) )
-( ((Ax-Cx)(By-Cy))-((Bx-Cx)(Ay-Cy)) ) > 0
This formula is taken from link text, and is based on a 0,0 centered circle.
The reason I am posting is that I am getting weird results and I wondered if I did something stupid. :(
although this doesn't exactly answer your question: Do you really have to calculate this on the fly on a SQL-Select? This means that the DB-system has to calculate the formula for every single row in the table (or every single row for which the remaining where conditions hold, respectively) which might result in bad performance.
Instead, you might consider creating a separate boolean column and calculate its value in an on-insert/on-update trigger. There, in turn, you wouldn't even need to put the test in a single line formula. Using a separate column has another advantage: You can create an index on that column which allows you to get your set of intersecting/non-intersecting records very fast.

Resources