Saturday, December 26, 2020

Rat-Maze problem - Yew app built with rustwasm

 Backtracking Algorithms

I just started loving the algorithms , especially backtracking algorithms.This geekforgeeks page gives more details on backtracking 

https://www.geeksforgeeks.org/rat-in-a-maze-backtracking-2/

Solving this problem in Rust programming  will be  a little more interesting. Even more fun if we can build this as  Rust Wasm app. Webassembly (wasm) is one of the latest technology that web developers are interested in. So thought of building  the rat-maze as a Yew app.

Yew is a modern Rust framework for creating multi-threaded front-end web apps with WebAssembly

Read mroe about Yew here - https://yew.rs/docs/en/


I used following dependencies - 

yew="0.17"
wasm-bindgen="0.2.67"
web-sys = "0.3.46"
wasm-logger = "0.2.0"
log = "0.4.6"


Backtracking -

This is one of the algorithm every programmer should try and understand. It will involve recursion and push and pop values from Results.

In my program, "solve" is the method actually implemented this backtracking

///Actual method where the backtracking algorithm is implemented
/// this method will be recursively called based the choices
/// we will be adding the "path" in this algorithm, before calling the method recursively
/// if we couldn't solve the Maze, pop the added "path" and next choice we have.


Yew Component:



///Grid is our component which displays the maze
/// Its also contains the path , once we solve it
struct Grid{
link:ComponentLink<Self>,
cell:[[u8;4];4],
path:Vec<(usize,usize)>,

}


The full program is below for your reference:

Full Code for Rat Maze


Output:




No comments: