Search code examples
javamultidimensional-arraytraversaldiagonaln-queens

Traversing Through a 2D Array diagonally from all sides


I'm currently working on N Queens problem where the input will be the size of the 2D array and the actual values of the 2D array. This code will check wether or not this input is valid, as in there aren't any other queens attacking each other, or if its not. If its valid you simply print out true else print out false. I'm about 95% done with my code, but I'm having trouble with traversing the 2D Array diagonally. I want to be able to check diagonally NE,NW,SE,SW but I keep getting array out of bounds on my code. I know why I keep getting it I just don't know how to fix it. I'm looking for some guidance on how to fix this issue. Here's my code.

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class nQueensMod {

public static int r,c;
public static int[][]board;

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    r = in.nextInt();
    c = in.nextInt();
    board = new int[r][c];
    for(int board_i=0; board_i < r; board_i++){
        for(int board_j=0; board_j < c; board_j++){
            board[board_i][board_j] = in.nextInt();
        }
    }

    System.out.println(solve(board,0,0));
}

   public static boolean solve(int[][]board, int row, int col)
   {
       if(row >= r )
         return true;

        if(board[row][col] == 1)
        {
              if(validRows(row,col) && validCols(col,row))
              {
                 if(move(row,col))
                    return true;
              }

          /*    
              if(validRows(row,col) && validCols(col,row) && validDiagonal(row,col))
              {
                 if(move(row,col))
                    return true;
              }
          */   
        }

           else
           {
              if(move(row,col))
                 return true;
           }

        return false;
   }

   public static boolean validRows(int row, int col)
   {
      for (int i = col + 1; i < r; i++)
      {
        if (board[row][i] == 1)
         {
           return false;
         }
       }

       return true;
   }

   public static boolean validCols(int cols, int row)
   {
      for (int i = row + 1; i < c; i++)
      {
        if (board[i][cols] == 1)
         {
           return false;
         }
       }

       return true;
   }

/*      
   public static boolean validDiagonal(int row, int cols)
   {
      for (int i = 1; i < c; i++)
      {

        if (
              //checks SE
              board[row + i][cols + i] == 1 ||
              //checks SW
              board[row + i][cols - i] == 1 ||
              //checks NE
              board[row - i][cols + i] == 1 ||
              //checks NW
              board[row - i][cols - i] == 1
            )

              return false; 
       }

       return true;
   }
*/      
   public static boolean move(int row,int col)
   {
        if(col < board.length - 1)
           return solve(board,row, col + 1);

           else
              return solve(board,row + 1, 0);
   }
 }

I commented out the actual validDiagonal method because that's what I tried but I keep getting the array out of bounds exception. That's the part I need help in. Here is a sample of the input

4 4 
0 1 0 0 
0 0 0 1
1 0 0 0
0 0 1 0

and Output

True

This is the error I get

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at nQueensMod.validDiagonal(nQueensMod.java:88)
at nQueensMod.solve(nQueensMod.java:40)
at nQueensMod.move(nQueensMod.java:108)
at nQueensMod.solve(nQueensMod.java:50)
at nQueensMod.main(nQueensMod.java:23)

This is my first time posting on here so I hope I posted the question the right way. Thanks in advance guys!


Solution

  • You have to check if your indices are in range before you access the array.

    I'll give you a hint,

    // for NW
    if (row-i>0 && col-i>0 && board[row - 1][cols - 1] == 1) 
        return true
    

    Do you see why there is an IndexOutOfBoundException?