-10

Here is my code for the eight queens problem. I checked it with many test case and it's correct. But when I submitted it to https://open.kattis.com/, it noticed me that my code is the wrong answer. So, where is my code fails? Please help!

public class Chesss {
  /**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws IOException {
    // TODO code application logic here
    int[] row = new int[8];
    int[] column = new int[8];
    int[] lcros= new int[15];
    int[] rcros = new int[15];
    for (int i = 0; i < 8; i ++) {
      row[i] = 0;
      column[i] = 0;
    }
    for (int i = 0; i < 15; i ++) {
      lcros[i] = 0;
      rcros[i] = 0;
    }
    boolean check = true;
    for (int i = 0; i < 8; i ++) {
      for (int j = 0; j < 8; j ++) {
        char in = (char)System.in.read();
        if (in == '\n') {
          in = (char)System.in.read();
        }
        if (in == '*') {
          if (row[i] == 1) {
            check = false;
          } else {
            row[i] = 1;
          }
          if (column[j] == 1) {
            check = false;
          } else {
            column[j] = 1;
          }
          if (lcros[i + j] == 1) {
            check = false;
          } else {
            lcros[i + j] = 1;
          }
          if (rcros[i - j + 7] == 1) {
            check = false;
          } else {
            rcros[i - j + 7] = 1;
          }
        }
      }
    }
    if (check == true) {
      System.out.print("valid");
    } else System.out.print("invalid");
  }
}
Derek Wang
  • 10,098
  • 4
  • 18
  • 39
trueterry
  • 1
  • 3
  • 5
    could you help us help you by giving meaningful names for your variables instead of a,b,c,d...? you know, it might even help you see the errors... – Sharon Ben Asher Sep 12 '16 at 06:38
  • 2
    and what is the input that you are getting? why do you need any input to solve the 8 queens problem ???? – Sharon Ben Asher Sep 12 '16 at 06:40
  • @sharonbn The program accepts a solution and validates it. – Marko Topolnik Sep 12 '16 at 06:44
  • 2
    This is **not** how this site works. We will not lookup that problem to figure what the input/outputs should be; and then figure what your program is doing wrong. You figure that yourself; and give that information; and then we might help with finding the root cause! – GhostCat Sep 12 '16 at 06:44
  • Oh, I submited my code to a website that have test cases to test my code, – trueterry Sep 12 '16 at 06:45
  • 1
    are you sure you are required only to validate a solution? if the website does not give you adequate information why your code is rejected, no one will do this for you. – Sharon Ben Asher Sep 12 '16 at 06:47
  • @MarkoTopolnik yes, that's right – trueterry Sep 12 '16 at 06:47
  • @sharonbn if the error is compiling error, ít will give me why my code is rejected, but wrong answer is not – trueterry Sep 12 '16 at 06:48
  • @trueterry, that is not what springs to mind when one reads "Here is my code for eight queens problem" – Sharon Ben Asher Sep 12 '16 at 06:48
  • What is row, what is column, what is lcros, what is rcros, what is the inputs? Does an input of * mean there's a queen? Leave commenting on what you are trying to do on each if statement – tt_Gantz Sep 12 '16 at 06:49
  • input is '*' is queen and '.' is space in chess board – trueterry Sep 12 '16 at 06:50
  • How does your lcross and rcross work? I get row and column are used to store if an queen is in that row or column, but why is lcross and rcross 15 in length? – tt_Gantz Sep 12 '16 at 06:56
  • 1
    For starters, you do not check if there really are eight queens on the board. – Florian Link Sep 12 '16 at 06:56

1 Answers1

1

Now that we know that your code is meant to "validate a solution to 8 queens problem", there are several problems with your code. here are the glaring most obvious two :

  1. You have to get the positions of ALL 8 queens BEFORE you start the validation process
  2. You have to check all the straight lines in all directions from the queens' positions, stopping at the edge of the chess board

I dont see all this in your code

Sharon Ben Asher
  • 13,849
  • 5
  • 33
  • 47
  • Have another look at the solution, its idea is to iteratively mark columns, rows and left or right diagonals as "blocked" and when it gets the position of a new queen it verifies that the position is not "blocked" and then updates the "blocked" fields. – Florian Link Sep 12 '16 at 07:06