Note that problem 82 may also be solved using a straightforward Dijkstra by adding an initial node A connected to all the nodes in the first column, and a final node B that all the nodes of the last column connect to, and then searching for a path from A to B.

Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down.

Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down.

Solution:

Solution:

+

+

A very verbose solution based on the Dijkstra algorithm. Infinity could be represented by any large value instead of the data type Distance. Also, some equality and ordering tests are not really correct. To be semantically correct, I think infinity == infinity should not be True and infinity > infinity should fail. But for this script's purpose it works like this.

I am offering this solution not because it is particularly
brilliant, but because it introduces the wonderful fgl Graph
Library written by Martin Erwig. Martin's Data.Graph.Inductive
library allows you to solve problems 81,82, and 83 with exactly
the same code, and best of all, little or no thinking. The idea
is to convert the n by n matrix into an n^2 by n^2 graph whose
edges depend on the allowed paths. Fortunately these graphs are
very sparse, averaging only 4 edges per node. This allows us to
use the Dijkstra algorithm to find the shortest path in a graph.

The only slightly dodgy bit is problem 82, where we must find
the shortest path from the first column to the last column. In
order to avoid recomputing the Dijkstra algorithm over and over
again, you have to be a little careful in the order of
evaluation. I used spTree function from
Data.Graph.Inductive.Query.SP which generated the shortest path
tree from a given initial node to all other nodes. I then map
over this tree with the nodes of the graph that are in the last
column. The tree only needs to be calculated once for each
element in the first column, rather than for every pair (i,j).
This reduces the running time by a factor of n.
Henry Laxen -- Apr. 27, 2008

Note that problem 82 may also be solved using a straightforward Dijkstra by adding an initial node A connected to all the nodes in the first column, and a final node B that all the nodes of the last column connect to, and then searching for a path from A to B.

Find the minimal path sum from the top left to the bottom right by moving left, right, up, and down.

Solution:

A very verbose solution based on the Dijkstra algorithm. Infinity could be represented by any large value instead of the data type Distance. Also, some equality and ordering tests are not really correct. To be semantically correct, I think infinity == infinity should not be True and infinity > infinity should fail. But for this script's purpose it works like this.

In the game, Monopoly, find the three most popular squares when using two 4-sided dice.

This may not be the shortest or the fastest implementation, but
I hope it is one of the clearest. I have one comment about the
experience of solving this problem that I would like to share
with you. At first I thought I would have to make use of the
Control.Monad.State library, but being relatively new to
Haskell, I quickly found myself in the slough of type checker
despond. It was then that I remembered that foldl/foldr can
used instead of "State," and now I found myself in the celestial
city of type checker heaven, with Haskell preventing me from
making silly mistakes at every turn. HenryLaxen May 7, 2008