title: Sudoku: checking 3x3 grids for repeating values
body: <p>I have worked for a sudoku puzzle in C but I'm stuck in one problem: Checking every 3x3 grid for not having duplicate values.
  Here is my code:</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  using namespace std; 
  int v[10][10];
  //Matrix start from 1 and end with 9
  //So everywhere it should be i=1;i&lt;=9 not from 0 to i&lt;9 !!!
  //Display function ( Display the results when it have)
  void afisare()
  {
      for(int i=1;i&lt;=9;i++){
      for(int j=1;j&lt;=9;j++)
      printf("%2d",v[i][j]);
      printf("\n");
  }
      printf("\n");
  }
  //Function to check the valability of value
  int valid(int k, int ii, int jj)
  {
      int i;
      //Check for Row/Column duplicate
      for(i = 1; i &lt;= 9; ++i) {
          if (i != ii &amp;&amp; v[i][jj] == k)
              return 0;
          if (i != jj &amp;&amp; v[ii][i] == k)
              return 0;
      }
      //Returns 0 if duplicate found return 1 if no duplicate found.
      return 1;
  }
  
  void bt()
  {
      int i,j,k,ok=0;
      //Backtracking function recursive
      for(i=1;i&lt;=9;i++){
      for(j=1;j&lt;=9;j++)
      if(v[i][j]==0)
      {
          ok=1;
          break;
      }
  
      if(ok)
      break;
      }
  
      if(!ok)
      {
          afisare();
          return;
      }
  
      for(k=1;k&lt;=9;k++)
      if(valid(k,i,j))
      {
          v[i][j]=k;
          bt();
      }
      v[i][j]=0;
  }
  int main()
  {
      //Reading from the file the Matrix blank boxes with 0
      int i,j;
      freopen("sudoku.in","r",stdin);
      for(i=1;i&lt;=9;i++)
      for(j=1;j&lt;=9;j++)
      scanf("%d",&amp;v[i][j]);
  
      bt();
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>I know in function Valid I should have the condition to check every 3x3 grid but I don't figure it out: I found those solution to create some variables start and end
  and every variable get something like this:</p>
  
  <pre><code>start = i/3*3;
  finnish = j/3*3;
  </code></pre>
  
  <p>i and j in my case are ii and jj.</p>
  
  <p>For example found something like this:</p>
  
  <pre><code>  for (int row = (i / 3) * 3; row &lt; (i / 3) * 3 + 3; row++)
      for (int col = (j / 3) * 3; col &lt; (j / 3) * 3 + 3; col++)
        if (row != i &amp;&amp; col != j &amp;&amp; grid[row][col] == grid[i][j])
          return false;
  </code></pre>
  
  <p>I tryed this code and it doesn't work.</p>
  
  <p>I don't understand this: I have the next matrix for sudoku:</p>
  
  <pre><code>1-1 1-2 1-3 1-4 1-5 1-6
  2-1 2-2 2-3 2-4 2-5 2-6
  3-1 3-2 3-3 3-4 3-5 3-6
  </code></pre>
  
  <p>If my code put's a value on 3-2 how he check in his grid for duplicate value, that formula may work for 1-1 or 3-3 but for middle values doesn't work, understand ?</p>
  
  <p>If my program get's to 2-5 matrix value It should check if this value is duplicate with 1-4 1-5 1-6 2-4 2-6 ... untill 3-6.</p>