package com.company; import java.util.*; /* * Eight Queens:Write an algorithm to print all ways of arranging eight queens on an 8x8 chess board * so that none of them share the same row, column, or diagonal. In this case, "diagonal" means all diagonals, * not just the two that bisect the board. * */ public class Main { private static int SIZE = 8; public static void main(String[] args) { // write your code here List<Integer[]> result = new ArrayList<>(); Integer[] col = new Integer[SIZE]; setQueen(result, col, 0); printList(result); } public static void setQueen(List<Integer[]> result, Integer[] col, int row) { if(row == SIZE) { result.add(col.clone());//这里一定要用clone 不然是同样的地址 return; } for(int i = 0; i < SIZE; i++) { if(isAvalible(row, col, i)) { col[row] = i; setQueen(result
Eight Queens Java 走地牙 CTCI 8.12
最新推荐文章于 2022-12-07 15:48:33 发布