将8个皇后问题从python转换为java:需要帮助

时间:2011-03-15 22:53:55

标签: java python

我刚开始学习java(今天),

为了锻炼我,我想将以下用python编写的“8 Queens”算法翻译成Java:

BOARD_SIZE = 8

def under_attack(col, queens):
    left = right = col
    for r, c in reversed(queens):
        left, right = left-1, right+1
        if c in (left, col, right):
            return True
    return False

def solve(n):
    if n == 0: return [[]]
    print n
    smaller_solutions = solve(n-1)
    return [solution+[(n,i+1)] for i in range(BOARD_SIZE) for solution in smaller_solutions if not under_attack(i+1, solution)]

sols = solve(BOARD_SIZE)
for answer in sols:
    print answer

对于翻译,我想使用完全相同的算法:递归和使用像python中的“元组列表列表”(我知道我应该想到“java”,但是现在,它只是为了好玩)

我写了这个:

import java.util.ArrayList;

class Queens {


    public static boolean under_attack(int col, ArrayList<Integer[]> queens) {
        int left = col, right = col;
        for(int i=queens.size()-1;i>=0;i--) {
            int r = queens.get(i)[0];
            int c = queens.get(i)[1];
            left--;
            right++;
            if ( c==left || c==col || c==right)  {
                return true;
            } 
        }
        return false;
    }

    public static ArrayList<ArrayList<Integer[]>> solve(int n){
        ArrayList<ArrayList<Integer[]>> new_solutions = new ArrayList<ArrayList<Integer[]>>();
        if ( n==0 ) {
            return new_solutions;
        }
        ArrayList<ArrayList<Integer[]>> smaller_solutions = solve(n-1);

        for (int i=0;i<8;i++) {
            for (ArrayList<Integer[]> solution : smaller_solutions) {
                if ( ! under_attack(i+1,solution) ) {
                   ArrayList<Integer[]> bigger_solution = (ArrayList<Integer[]>) solution.clone();
                   Integer [] tuple = new Integer [2];
                   tuple[0] = n;
                   tuple[1] = i+1;                   
                   bigger_solution.add(tuple);
                   new_solutions.add(bigger_solution);
                }
            }
        }
        return new_solutions;

    }

    public static void main(String[] args) {
        System.out.println("Résolution du problème des 8 reines");
        ArrayList<ArrayList<Integer[]>> solutions;
        solutions = solve(8);
        System.out.format("Nb solutions : %d%n",solutions.size());
        for (ArrayList<Integer[]> solution : solutions) {
            System.out.print("(");          
            for(Integer[] i:solution) {
                System.out.format("[%d,%d],",i[0],i[1]);            
            }
            System.out.println(")");        
            System.out.println("==============================");       
        }       
    }    
}

但这不起作用:找不到答案

你知道为什么吗?

1 个答案:

答案 0 :(得分:1)

您的代码运行以正确运行python程序所需的更正如下。在solve()函数的开头而不是:

    if ( n==0 ) {
        return new_solutions;
    }

你应该写:

    if ( n==0 ) {
        ArrayList<Integer[]> empty = new ArrayList<Integer[]>();
        new_solutions.add(empty);
        return new_solutions;
    }

原因是python中的工件[[]]不是空列表,对吧?它是一个列表,其中包含空列表的唯一元素。这正是java代码中的修正!否则递归不起作用,永远不会调用under_attack()函数(因为您可以验证在第一行添加诊断消息)

快乐编码......和java学习