Pathfinding with destructible obstacles - algorithm

I'm currently planning a game, but I have a hard time figuring the path finding algorithm and I was wondering if a generous soul would be able to help me with that.
Here's the scenario, I need an algorithm that calculate if it would be better to walk around obstacle or destroy them. I gave a "difficulty" to each tile of my game. For exemple a basic tile is 1 and obstacle can be between 5-100.
Here is some exemples. I must move from Red Square to Blue square. If I put an obstacle on the way I should get something like this :
Explanation : Left or Right path is only 3 difficulty and the obstacle is 5. So it's better to walk around.
Second example :
Explanation : The algorithm as 3 choice, break the obstacle or walk around by left or right because it's the same difficulty level.
Last example :
Explanation : The algorithm must be able to find a "weak spot" and be able to walk to it and destroy it.
I'll continue to work on it. Hope you can give me some guidance.

You can use Dijkstra's algorithm. To use the algorithm we'll define the following graph. Each square will be a node in the graph. Between two adjacent squares we'll define 2 directed edges. The weight of an edge will be the number written on the pointed node (e.g. for an edge (a,b) the weight will be the number written on square b).
Then, by running Dijkstra's algorithm on the graph defined above we'll get the shortest path.

You can do this with a very minor variation (actually no variation at all, you just interpret differently what is an arc cost) of Dijkstra's algorithm.
Basically, you can reach the "inside" of the obstacled square at the cost of its destruction, so you have a graph where all nodes are connected to all their neighbours (i.e., the obstacle does not "disconnect" the intervening nodes).
So in this case, going from A to B,
1 1 A 1 1 1
1 1 1 1 1 1
2 5 5 5 2 1
1 1 B 1 1 1
the costs would be
2 1 A 1 2 3
3 2 1 2 3 4
5 7 6 7 5 5
6 7 B 7 6 6
and you would go straight downwards.

Related

Is 2 times Manhattan distance still an admissible in N puzzle searching problem using A star?

I know Manhattan distance is an admissible heuristic function since it doesn't overestimate the cost of moving a tile to the correct location. But my question is
If I double h, say scale up each of the Manhattan distances by a factor of 2, or the factor of the corresponding tile value (e.g., h'=9*h if we are moving tile 9, 2*h if we move 2).
Is it still admissible? My feeling is it's not since it overestimate the true cost. So for this specific problem, is Manhattan distance the most dominant heuristic function possible?
taking this as an example, starting with
1 2 3
4 5 6
7 0 8
we want
1 2 3
4 5 6
7 8 0
Apparently we only need one move (swap 0 and 8) and guaranteed to find a solution, so admissible or non admissible at this point doesn't matter. But in general, does 2*MD an admissible heuristic function? How do we justify this?

Find shortest path in 2D weighted grid

I have a 2D grid, somtehing like this, but a little bit bigger
A 1 2 2 2
2 1 X 2 1
1 X X 1 2
1 2 X 1 1
2 2 1 B 2
I want to find the shortest path from A to B. Where is X, there is not possible to go. From 1 to 2 is cost 2, from 2 to 1 is cost 1. I can move horizontally and vertically.
I imagined it like graph with evaluated vertexes and then I tried transform it to graph with evaluated edges. Then i wanted to apply dijkstra algorithm on it. In one article I read, that it is possible to apply dijkstra directly to grid.
My question is if it is possible to apply dijkstra algorithm directly to weighted grid? If no, how can I make a graph from grid or adjacency matrix? But adjacency matrix is probably too large for my inputs.
Thank you very much for your answers :)

Algorithm to sort numbers

I have different numbers which needs to be sorted in ascending order by exchanging each number with 0.
The resultant number should be obtained only from exchanging each number with 0.
for eg, I have 3x3 matrix,
3 4 1
2 5 0
6 8 7
In above matrix, the number should be exchanged with 0 to make an ascending order. From above, only 5,7 and 1 are exchanged with 0 at first step. The final output should be like this
1 2 3
4 5 6
7 8 0
What is an optimal solution to achieve this.
Thanks
This is an easier version of the famous 15-puzzle.
The general way to approach such a problem is to model it as states graph, and run a shortest-path algorithm in order to find a path from the source (given board) to the target (sorted board).
The states graph is G=(V,E) where: V= { all possible boards } and E={(u,v) | can change board u to v with a single swap }`.
You can run BFS or bi-directional BFS (since you have one source and one target), or even A* Algorithm with an appropriate admissible heuristic function to find the path on the states graph, which represents a series of swaps that yield a solution.

Using disjoint-set data structure, graph theory

I'm practicing solving programming problems in free time. This problem I spotted some time ago and still don't know how to solve it:
For a given undirected graph with n vertices and m edges (both less than 2 × 106)
I need to split its vertices into as many groups as possible, but with one
condition: each pair of vertices from different groups are connected by edge.
Each vertex is in exactly one group. At the end I need to know the size of
each group.
I was proud when I came up with this solution: consider complemented graph of the original graph and use Disjoint-set data structure for it. It gives us the right answer (not difficult to prove). But it's only theoretical solution. With given constraints it's very very bad, not optimal. But I believe this approach can be somehow smartly fixed. But how?
Can anyone help?
EDIT: for a graph with vertices from 1 to 7 and 16 edges:
1 3
1 4
1 5
2 3
3 4
4 5
4 7
4 6
5 6
6 7
2 4
2 7
2 5
3 5
3 7
1 7
we have 3 groups with sizes: 1, 2 and 4.
These groups are: {4}, {5,7}, {1,2,3,6} respectively. There are edges connecting each pair of vertices from different groups and we can't create more groups.
I think the only ingredient you're missing is how to deal with sparse graphs.
Let's think about this in terms of finding the biggest possible complete graph where the only operation I can do is group a set of nodes (say v_1, ..., v_k) together and give the new supernode edges only to those nodes u that were connected to all of v_1, ..., v_k.
If your graph has fewer than n^2/4 edges, randomly sample n node pairs, noting which pairs are not joined by an edge. Union-find is an easy way to code this up. Now rebuild the graph using as groups the sets you found by this random sampling. Recurse on this reduced graph. (I'm not quite sure how to analyse this step, but I believe each sample-rebuild cycle reduces the graph size by at least a constant factor with high probability, so this whole process takes near-linear time.)
Once you have a fairly dense graph (at least n^2/4 edges), you can convert to an adjacency matrix representation and do exactly what you were suggesting --- check all node pairs, do a union whenever you see that two nodes aren't joined by an edge, and read off the sets.

Are paths in graph connected

I have a problem with paths in graph. We have a graph, for example:
5 verticles and 4 edges
1 2 first is connected to second, etc
2 3
3 4
5 1
And now I would like answer on questions(for example):
If vertex 1 is conneted to
vertex 3. Answer is YES - becauese we have path" 1 -> 2 -> 3.
What Do you advise me?
I have no idea how to do it.
This will require some research on your part. The idea is to use a graph traversal algorithm like depth-first or breadth-first. Start from a vertex (like 1 in your example) and keep traversing the graph until you either reach the target node (3 in your example) or you cannot find any more paths to follow.
DFS or BFS (I'd prefer DFS because it would result in less backtracking) from the starting node, if the algorithm completes without finding the node it is not reachable.

Resources