fork download
  1. class Neigbors{
  2. public static void main(String []args){
  3. int COL_SIZE = 4;
  4. int ROW_SIZE = 4;
  5. for (int currentBombIndex = 1; currentBombIndex < ((COL_SIZE * ROW_SIZE) + 1); currentBombIndex++) {
  6. int currentBombCol = ((currentBombIndex - 1) % COL_SIZE) + 1;
  7. int currentBombRow = ((currentBombIndex - 1) / COL_SIZE) + 1;
  8. System.out.println("Neighbors of " + currentBombIndex + " (" + currentBombRow + ", " + currentBombCol + ")");
  9. for (int x = -1; x <= 1; x++) {
  10. for (int y = -1; y <= 1; y++) {
  11. if (x == 0 && y == 0) {
  12. continue; // Current bomb index
  13. }
  14. int neighborCol = currentBombCol + y;
  15. int neighborRow = currentBombRow + x;
  16. if (neighborCol > 0 && neighborRow > 0 && neighborCol <= COL_SIZE && neighborRow <= ROW_SIZE ) {
  17. int computedNeighborIndex = neighborCol + ((neighborRow - 1) * COL_SIZE);
  18. System.out.print(computedNeighborIndex + " ");
  19. }
  20. }
  21. }
  22. System.out.println();
  23. }
  24. }
  25. }
  26.  
Success #stdin #stdout 0.1s 320320KB
stdin
Standard input is empty
stdout
Neighbors of 1 (1, 1)
2 5 6 
Neighbors of 2 (1, 2)
1 3 5 6 7 
Neighbors of 3 (1, 3)
2 4 6 7 8 
Neighbors of 4 (1, 4)
3 7 8 
Neighbors of 5 (2, 1)
1 2 6 9 10 
Neighbors of 6 (2, 2)
1 2 3 5 7 9 10 11 
Neighbors of 7 (2, 3)
2 3 4 6 8 10 11 12 
Neighbors of 8 (2, 4)
3 4 7 11 12 
Neighbors of 9 (3, 1)
5 6 10 13 14 
Neighbors of 10 (3, 2)
5 6 7 9 11 13 14 15 
Neighbors of 11 (3, 3)
6 7 8 10 12 14 15 16 
Neighbors of 12 (3, 4)
7 8 11 15 16 
Neighbors of 13 (4, 1)
9 10 14 
Neighbors of 14 (4, 2)
9 10 11 13 15 
Neighbors of 15 (4, 3)
10 11 12 14 16 
Neighbors of 16 (4, 4)
11 12 15