Tales Igor Ebert

Tales Igor Ebert – Projetos de Universidade

Algorithm implementation for Solution through “backtracking”

The problem of “Military – Explore territory”!

Scenario: A military man has the important task of entering a hostile territory, exploring all the possibilities of existing paths and mapping a safe path to a final coordinate. There are obstacles that prevent him from pursuing, he needs to check if paths allow passage. He needs to deactivate the land mines that exist along that path. The main objective of the military is to bring back a map indicating all and the best safe route, marking for the other military the free passage coordinates and the location of each mine found and deactivated.

Backtracking: To solve this problem it is necessary to implement a backtracking and recursive algorithm. The proposal is similar to the implementations of the proposed labyrinth solutions with small adjustments to satisfy the initial proposal.

Results: The recursive backtracking algorithm runs from the position x (1), y (1), all possible paths with the order (north, south, east and west), marking the position as 3 for path used, 7 for Mine and 8 for final position. Every time you find a dead-end path, the recursion will drop to the last valid path.

See complete text PDF on: https://docs.google.com/document/d/1HRbpfoH_UrKZf4glPlNo_NGuJrUQGye1bGWqRvydKHE/pub

Input Matrix:
 {{1, 1, 1, 1}, // first state
 {1, 0, 0, 1},
 {1, 9, 0, 1},
 {1, 0, 4, 1},
 {1, 1, 1, 1};
 where the walls are represented by 1, the paths free by 0, 4 as the end, and the mines by number 9.

The algorithm will mark the valid path with 3, mines found by 7, and the end by 8.

At the end, all valid states (paths found) are stored in a list of states in the Search class. If you need to debug, enable the enable_debug attribute using the commented //first.enableDebug () method;

The FindBest class is just to find the best path.

Code on github: https://github.com/talesebert/backtraking_maze_mine

compile: javac Search.java State.java FindBest.java
run: java Search

The final output result is something like this:

We found 4 path(s)! Enable debug to view all paths.
 1 1 1 1
 1 3 3 1
 1 9 3 1
 1 0 8 1
 1 1 1 1
The best path :) with only: cost 3 and travaled 4Km

Junho 18th, 2017

Posted In: Recursive algorithm

Leave a Reply