Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: None  |  size: 0.62 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Visiting all locations in a multidimensional array directly adjacent to some location?
  2. XXX  
  3. XOX  
  4. XXX
  5.        
  6. for (int i = x - 1; i <= x + 1; i++) {
  7.     for (int j = y - 1; j <= y + 1; j++) {
  8.         /* Skip the point itself! */
  9.         if (i == x && j == y) continue;
  10.  
  11.         /* Process the location here */
  12.     }
  13. }
  14.        
  15. for (int i = x - 1; i <= x + 1; i++) {
  16.     for (int j = y - 1; j <= y + 1; j++) {
  17.         if ((i == x) == (j == y)) continue;
  18.  
  19.         /* Process the location here */
  20.     }
  21. }
  22.        
  23. array[i-1][j-1];
  24. array[i-1][j];
  25. array[i-1][j+1];
  26. array[i][j-1];
  27. array[i][j+1];
  28. array[i+1][j-1];
  29. array[i+1][j];
  30. array[i+1][j+1];