Reputation: 709
I have a grid, The Grid have two "Material" -
For example :
In this grid we have objects that have size and location (The location of an object is the top left point).
On each object we can do some action like -
I need to make a function that returns me the Minimum amount of actions I need to do on a object to move it from one point to another point (I need only the amount of action).
I solved this problem using dijkstra's algorithm, but without the turn action.
So can anyone help me to build this function.
Example of the problem -
The starting point -
The end point
And I need to return the minimum amount of actions I need to do on a object.
Upvotes: 5
Views: 1239
Reputation: 621
For your problem, I think BFS still works.
Using BFS to solve traditional Maze problem, starting from the beginning point, you have to:
1. Enqueue every point that is accessible (not a piece of wall, and not visited) and connected to the current point.
2. Dequeue current point and mark it as VISITED.
From above, you can see that BFS won't let any point to be visited twice, thus loop is avoided.
As for your problem, BFS still works, but we will slightly change the definition of "accessible":
First, I will introduce what the "maze" matrix looks like. Here is what the numbers mean in the following images.
(Assume that the size of the object is 1*2, and when moving, the top-left corner of the object stays at each point).
00: The point can't be accessed, neither the object is horizontal nor vertical.
10: The point can be accessed if the object is horizontal
01: The point can be accessed if the object is vertical
11: The point can be accessed if the object is either horizontal or vertical
And your graph can be converted into a matrix like below:
Fill in those inaccessible points with 00, and you'll get
This is more like a Maze problem, but a little bit different.
Finally, let's see how to "access" those points:
The definitions of connected
is similar to the traditional Maze problem. Here are some examples:
---------
| 01| 10|
|---|---|
| 10| |
--------- (Not Connected from top-left to either top-right or bottom-left)
---------
| 11| 10|
|---|---|
| 01| |
--------- (Connected from top-left to both top-right and bottom-left)
---------
| 10| 10|
|---|---|
| 01| |
--------- (Connected from top-left to top-right, but not connected to bottom-left)
So the rest may be simple. Follow the traditional BFS method, and create a 2-dimension array to store the length of the shortest path at each point. Dequeue to get the current point, add connected
neighbors of current point to the queue, and mark this point as VISITED, then everything will be the same as BFS.
After you've got the shortest path, re-run your program, maintain a state of whether the object is vertical or horizontal at current point, and simulate your move on the image. Add turns only when necessary, and you will get the result with turns added.
Upvotes: 2
Reputation: 10565
Consider the problem as finding the shortest path in a 3D grid, with a depth of 2 (each of the possible states: horizontal and vertical). You have to code the constraints that disallows one from moving from one depth to another, e.g., cannot go vertical if it doesn't fit that way.
Now you can just use regular BFS to find the shortest path in a grid (i.e., an unweighted graph).
Upvotes: 5
Reputation: 1321
If I understand what you're asking, writing this out would take too long, but the easiest way of doing it I can think of is finding the path first, check for collision along that path, rotate, and add the actions accordingly.
With a more complicated grid, this would obviously get more complicated, but you only have to do 90 degree turns. It should be possible in ~9 actions.
Upvotes: 0
Reputation: 306
Use the Breadth-first search strategy, assigning weights to edges depending on must turn the object or not.
Upvotes: -2