stevensonmt
I’m really baffled by this leetcode problem. My algorithm finds a valid path but it is not guaranteed to be the shortest path as it will include corners that could be cut diagonally (though not always). Any help?
defmodule Solution do
@neighbors [{1, 1}, {1, 0}, {0, 1}, {-1, 1}, {1, -1}, {0, -1}, {-1, 0}, {-1, -1}] |> MapSet.new()
@empty_q :gb_sets.empty()
@spec shortest_path_binary_matrix(grid :: [[integer]]) :: integer
def shortest_path_binary_matrix(grid) do
n = length(grid) -1
valid_pts =
grid
|> Enum.with_index()
|> Enum.flat_map(fn {row, i} ->
row
|> Enum.with_index()
|> Enum.reject(fn {v, _} -> v == 1 end)
|> Enum.map(fn {_, j} -> {j, i} end)
end)
|> Enum.reduce(Map.new(), fn pt, map -> Map.put(map, pt, -1) end)
if [{0,0}, {n, n}] |> Enum.any?(fn pt -> not Map.has_key?(valid_pts, pt) end) do
-1
else
visited = MapSet.new()
q = :gb_sets.insert({0, 0, 1}, @empty_q)
path_finder(Map.put(valid_pts, {0, 0}, 1), visited, q, n, 1)
end
end
def path_finder(_, _, @empty_q, _, distance), do: -1
def path_finder(pts, visited, queue, target, distance) do
{{x, y, d}, sub_q} = :gb_sets.take_smallest(queue)
cond do
MapSet.member?(visited, {x, y}) ->
path_finder(pts, visited, sub_q, target, distance)
{x, y} == {target, target} ->
IO.inspect(visited, label: "path in the end")
d
true ->
v = MapSet.put(visited, {x, y})
ns = neighbors({x,y}, target) |> Enum.reject(&MapSet.member?(visited, &1))
{pts, queue} =
ns
|> Enum.reduce({pts, sub_q}, fn {a, b} = n, {map, q} ->
case map[n] do
x when x < d + 1 ->
{
Map.put(map, n, d + 1),
:gb_sets.add_element({a, b, d + 1}, q)
}
_ -> { map, q }
end
end)
path_finder(pts, v, queue, target, d + 1)
end
end
def neighbors({x,y}, n) do
@neighbors
|> Enum.map(fn {a, b} -> {a + x, y + b} end)
|> Enum.reject(fn {a, b} -> a > n or b > n end)
|> Enum.reject(fn {a, b} -> a < 0 or b < 0 end)
end
end
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 4- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
lud
I started with A* but we need Dijkstra here so maybe there are remnants left. Anyway this is a quick solution:
If your algorithm fails to prioritize diagonals over corners maybe there is a misuage of :gb_sets ? Because
take_smallestwill return the lowest{x, y, cost}tuple, and that means{0,999,999}is lower than{1,1,1}.stevensonmt
Thanks so much for your help. That was intentional but misguided. I was thinking I wanted to prioritize the closest node to the last node, which is not smart. Just changing the structure of the
gb_setto{distance, x, y}fixed my issue. So happy I’ll be able to sleep tonight.Werner
BTW if you’re interested in Maze’s in general, there’s also a book by Pragmatic (although I have not read it, but is on my list):
Mazes for programmers
and a nice project by Angelika Tyborska:
https://github.com/angelikatyborska/mazes
stevensonmt
That book seems fun. I will definitely check it out. Thanks!