title: C++ code example: What makes this loop so many times?
body: <p>I found a code example here: <a href="http://techmyway.wordpress.com/2011/11/09/small-documented-c-code-for-backtracking-problem-n-queens/" rel="nofollow">N-QUEEN Backtracking problem solved</a></p>
  
  <p>It shows this code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  /** this is the size of chess board */
  #define N 8
  
  /** Given a completed board, this prints it to the stdout */
  void print_solution(int state[N][N])
  {
      int i,j;
      for (i = 0; i &lt; N; ++i)
      {
          for (j = 0; j &lt; N; ++j)
              cout &lt;&lt; state[i][j] &lt;&lt; " ";
          cout &lt;&lt; endl;
      }
      cout &lt;&lt; endl;
  }
  
  /** return true if placing a queen on [row][col] is acceptable
  else return false */
  bool accept(int state[N][N], int row, int col)
  {
      int i,j;
  
      /* check the column */
      for (i = 0; i &lt; N; ++i)
      {
          if (state[row][i])
              return false;
      }
  
      /* check the row */
      for (i = 0; i &lt; N; ++i)
      {
          if (state[i][col])
              return false;
      }
  
      /* check the upper left diagnol */
      for (i = row, j = col; i &gt;= 0 &amp;&amp; j &gt;= 0; i--, j--)
      {
          if (state[i][j])
              return false;
      }
  
      /* check the lower left diagnol */
      for (i = row, j = col; i &lt; N &amp;&amp; j &gt;= 0; ++i, --j)
      {
          if (state[i][j])
              return false;
      }
  
      /* check the upper right diagnol */
      for (i = row, j = col; i &gt;= 0 &amp;&amp; j &lt; N; i--, ++j)
      {
          if (state[i][j])
              return false;
      }
  
      /* check the lower right diagnol */
      for (i = row, j = col; i &lt; N &amp;&amp; j &lt; N; ++i, ++j)
      {
          if (state[i][j])
              return false;
      }
  
      /* return true if all tests passed */
      return true;
  }
  
  void solve_state(int state[N][N], int row)
  {
  
      int i;
  
      /* if all queens have been placed at non conflicting positions */
      if (row == N)
      {
  
          print_solution(state);
          return;
      }
  
      /* Place queens on all positions in a given row and
      check if the state is acceptable
      Continue the process till all possibilities found */
      for(i=0; i&lt;N; ++i){
  
          if(accept(state, row, i)){
              state[row][i] = 1;
              solve_state(state, row+1);
          }
          state[row][i] = 0;
      }
  }
  
  int main()
  {
      /* initialise the board */
      int state[N][N] = {0};
  
      solve_state (state, 0);
  
      return 0;
  }
  </code></pre>
  
  <p>I fail rather hard with logic and I have even more trouble trying to learn recursion and such. What really ticks me off is that I just cannot comprehend why this code loops through all the 92 solutions of the 8 queens in chess problem. At first I thought it only found one solution but I was surprised once I tested the code and it ran all the possible solutions. </p>
  
  <p>I must be missing something very basic here because I've even attempted to make it "stop" after just one solution but I just fail.</p>
  
  <p>So I suppose what I am asking here is that in order to understand this, given this code, if I just wanted it to <strong>loop through once, and find the first solution, what would need to be changed?</strong> What kind of sorcery makes this thing loop all these times?! </p>