title: Topic: Intuition behind using backtracking (and not just recursive DFS)
body: <p>For starters, I am not trying to ask <a href="https://stackoverflow.com/a/26671095/8600195">the difference between a car and a DeLorean</a>.  So, I am solving <a href="https://leetcode.com/problems/word-search/" rel="nofollow noreferrer">this</a> LeetCode question:</p>
  
  <blockquote>
    <p>Given a 2D board and a word, find if the word exists in the grid. 
    The word can be constructed from letters of sequentially adjacent cell, where "adjacent" cells are those horizontally or vertically neighboring. The same letter cell may not be used more than once.</p>
    
    <p>board =<br>
    [<br>
    &nbsp;&nbsp;&nbsp;  ['A','B','C','E'],<br>
    &nbsp;&nbsp;&nbsp;  ['S','F','C','S'],<br>
    &nbsp;&nbsp;&nbsp;  ['A','D','E','E']<br>
    ]<br></p>
    
    <p>Given word = "ABCCED", return true.</p>
  </blockquote>
  
  <p>A <a href="https://leetcode.com/problems/word-search/discuss/27658/Accepted-very-short-Java-solution.-No-additional-space./26701" rel="nofollow noreferrer">highly upvoted solution</a> is as follows:</p>
  
  <pre><code>public class Solution {
  public boolean exist(char[][] board, String word) {
      for(int i = 0; i &lt; board.length; i++)
          for(int j = 0; j &lt; board[0].length; j++){
              if(exist(board, i, j, word, 0))
                  return true;
          }
      return false;
  }
  private boolean exist(char[][] board, int i, int j, String word, int ind){
      if(ind == word.length()) return true;
      if(i &gt; board.length-1 || i &lt;0 || j&lt;0 || j &gt;board[0].length-1 || board[i][j]!=word.charAt(ind))
          return false;
      board[i][j]='*';
      boolean result =    exist(board, i-1, j, word, ind+1) ||
                          exist(board, i, j-1, word, ind+1) ||
                          exist(board, i, j+1, word, ind+1) ||
                          exist(board, i+1, j, word, ind+1);
      board[i][j] = word.charAt(ind);     //--&gt; why?
      return result;
  }
  </code></pre>
  
  <p>My question is - what was the intuition behind using a backtracking algo for this question, as against using a normal recursive DFS?  While using recursive DFS, I would just have marked the nodes as visited and then moved on to its neighbors (thereby figuring out <code>ABCCED</code> is a valid path).  Why do I have to <em>backtrack</em> (commented line in code above) to realize if this path exists?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong>: Other way of asking my question is this way:  Why don't we start from the topmost left cell <code>A</code> and start visiting all its neighbors using a <code>visited</code> set along the way to mark visited nodes?  In the next iteration, we could start from the cell adjacent to topmost left <code>A</code> - <code>B</code>, visit all its neighbors using a new <code>visited</code> set to mark visited nodes and so on?  Why use backtracking?</p>