title: Creating adjacency matrix from 2D array
body: <p>I have an array like the following:</p>
  
  <pre><code>a b c d 
  e f g h
  j k l m
  n o p q
  </code></pre>
  
  <p>My idea is to create an adjacency matrix from this for only horizontal and vertical movement, where the costs are the ASCII value in the destination.</p>
  
  <p>The solution would find the following kind of adjacency matrix (simplified 'a'=1):</p>
  
  <pre><code>  a b c d e f g h &lt;- start
  a 0 1 0 0 1 0 0 0 
  b 2 0 2 0 0 2 0 0
  c 0 3 0 3 0 0 3 0 
  d 0 0 4 0 0 0 0 4
  e 5 0 0 0 0 5 0 0
  f 0 6 0 0 6 0 6 0
  g 0 0 7 0 0 7 0 7
  h 0 0 0 8 0 0 8 0
  ^ destination
  </code></pre>
  
  <p>I removed the last two rows of the original matrix for brevity. 
  I've come as far as to realize that a row has only one specific cost, and has a maximum of 4 adjacencies. My question is how do I find these adjacencies? If possible, I want to only iterate through the original matrix to save myself from using the exponentially larger adjacency matrix.</p>