java - 如何正确转换 n 皇后数组输出

标签 java arrays tostring n-queens

我已经研究这段代码有一段时间了。我想做的主要事情是对输出进行实验。这是代码来源:1

他们使用的java代码如下:

public class Queens {

   /***********************************************************************
    * Return true if queen placement q[n] does not conflict with
    * other queens q[0] through q[n-1]
    ***********************************************************************/
    public static boolean isConsistent(int[] q, int n) {
        for (int i = 0; i < n; i++) {
            if (q[i] == q[n])             return false;   // same column
            if ((q[i] - q[n]) == (n - i)) return false;   // same major diagonal
            if ((q[n] - q[i]) == (n - i)) return false;   // same minor diagonal
        }
        return true;
    }

   /***********************************************************************
    * Print out N-by-N placement of queens from permutation q in ASCII.
    ***********************************************************************/
    public static void printQueens(int[] q) {
        int N = q.length;
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++) {
                if (q[i] == j) System.out.print("Q ");
                else           System.out.print("* ");
            }
            System.out.println();
        }  
        System.out.println();
    }


   /***********************************************************************
    *  Try all permutations using backtracking
    ***********************************************************************/
    public static void enumerate(int N) {
        int[] a = new int[N];
        enumerate(a, 0);
    }

    public static void enumerate(int[] q, int n) {
        int N = q.length;
        if (n == N) printQueens(q);
        else {
            for (int i = 0; i < N; i++) {
                q[n] = i;
                if (isConsistent(q, n)) enumerate(q, n+1);
            }
        }
    }  


    public static void main(String[] args) {
        int N = Integer.parseInt(args[0]);
        enumerate(N);
    }

}

我的问题是这样的:我想知道如何使输出更加精简。换句话说,如何进行输出,以便将每个可能的解决方案的皇后的行位置列在一组括号中?例如,假设我有一个 4x4 板,如下所示:

xxQx
Qxxx
xxxQ
xQxx

and instead of having the above output, I want something like: (2,4,1,3), where "2" exemplifies the queen in the second row (the first one read when looking from right to left), "4" represents the queen in the 4th row, etc.. Since I'm relatively new to programming, I'm not too sure how to go about this. This is what I had actually tried in the printQueens part of the code:

public static void printQueens(int[] q) {
    int N = q.length;
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            if (q[i] == j)
                int[] rowPos = new int[1+j];
                System.out.print(Arrays.toString(rowPos));

        }

        System.out.println();

    }
    System.out.println();
}
正如你所看到的,在“if”语句下,我创建了一个新数组,我希望包含皇后所在的行号,然后我想将其作为字符串打印出来。不过,我在这方面遇到了错误,但我不知道为什么。例如,我知道我想要获得的输出不会看起来像 (x,y,z),但我只是想打印此时皇后所在的行,然后担心后面加上逗号和括号。但是,如果您能帮助我解决这些问题,那就太好了。错误如下所示:

/image/Ydlfk.png

编辑:我进行了必要的更改,但现在我的输出如下所示: 1

不知道为什么它根据数组的行数来打印它。我只需要每个解决方案有一个输出来显示皇后的行号

最佳答案

if (q[i] == j)
    int[] rowPos = new int[1+j];
    System.out.print(Arrays.toString(rowPos));

如果这应该是一个 if block ,则需要使用大括号。

if (q[i] == j) {
    int[] rowPos = new int[1+j];
    System.out.print(Arrays.toString(rowPos));
}

但是您的 rowPos 数组将始终只保存零,因为您没有在其中放入任何内容。

编辑:

如果您只想打印 q 的内容(这似乎就是您所说的内容),您可以摆脱循环并执行以下操作:

public static void printQueens(int[] q) {
    System.out.println(Arrays.toString(q));
}

如果您需要打印位置,就好像它们是从 1 而不是从 0 索引一样,您可以这样做:

public static void printQueens(int[] q) {
    int[] adjusted = new int[q.length];
    for (int i = 0; i < q.length; ++i) {
         adjusted[i] = q[i] + 1;
    }
    System.out.println(Arrays.toString(adjusted));
}

关于java - 如何正确转换 n 皇后数组输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30462804/

相关文章:

c++ - to_string 不是 std 的成员,g++ (mingw)

java - JPQL 查询中的静态工厂方法而不是构造函数

java - 在java中,当找到字符串匹配时如何打印文件中的整行

c# - 将数组转换为字符串

python - C# Unity - 从统一的相机中提取像素数组并在 python 中显示

java - 测试 toString 但失败?

php - symfony2 ContextErrorException : Catchable Fatal Error: Object of class Proxies\__CG__\. ..\Entity\... 无法转换为字符串

java - for 循环的问题,接收和利用输入

java - org.eclipse.ui.menus 的名称过滤器 - 也适用于编辑器 View

javascript - 将 javascript 中的数组更改为更简单的对象