java中的八个皇后

时间:2016-09-12 06:35:00

标签: java n-queens

这是我的八个皇后问题的代码。我用很多测试用例检查了它,这是正确的。但当我将其提交给https://open.kattis.com/时,它注意到我的代码是错误的答案。那么,我的代码在哪里失败了?请帮忙!

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");
}

}

1 个答案:

答案 0 :(得分:1)

现在我们知道您的代码旨在“验证8个皇后问题的解决方案”,但您的代码存在一些问题。这里有明显最明显的两个:

  1. 您必须获得所有 8位女王的位置之前才能开始验证过程
  2. 你必须从女王的位置检查所有方向的所有直线,停在国际象棋棋盘的边缘
  3. 我在你的代码中没有看到这一切

相关问题