题目来源:厦门大学研究生课 计算智能举例 主函数部分 import java.util.ArrayDeque; import java.util.HashSet; import java.util.Queue; import java.util.Set; public class Main { private Queue workingList = new ArrayDeque<>();//用队列实现广度优先遍历 private Set visitedNode = new HashSet<>();//该容器中只能存储不重复的对象 private final Node head = new Node(10, 0, 0);//定义初始状态 private final Node target = new Node(5, 5, 0);//定义最终状态 private void printRoute(Node node) { //用递归实现对所要路径的输出 if (node.getParent() != null) { //如果此节点的父节点为空,那么就可以输出该点,否则就对他的父节点执行输出操作 printRoute(node.getParent()); System.out.println(node); } else { System.out.println(node); } } public void solve() { workingList.add(head); //最先开始在队列中加入头结点 while (true) { if (workingList.isEmpty()) { System.out.println("FAIL"); return; //队列空则退出 } Node node = workingList.poll(); //得到工作队列的第一个元素 if (node == null) { System.out.println("FAIL"); return; //如果节点为空,就输出错误信息 } if (node.equals(target)) { //如果是目标点,就输出序列 printRoute(node); return; } if (visitedNode.contains(node)) { //如果这个节点已经访问过,就跳过此点,直接下一次循环 continue; } visitedNode.add(node); //加入到已访问节点序列 if (node.getA() >= 0) { workingList.add(new Node(0, 7, 3, node)); //以下是12中规则,用来创造新的子节点 } if (node.getA() <= 10) { workingList.add(new Node(10, 0, 0, node)); } if (node.getB() > 0 && node.getA() + node.getB() < 10) { workingList.add(new Node(node.getA() + node.getB(), 0, node.getC(), node)); } if (node.getB() > 0 && node.getB() + node.getC() <= 3) { workingList.add(new Node(node.getA(), 0, node.getB() + node.getC(), node)); } if (node.getB() > 0 && node.getB() + node.getC() > 3) { workingList.add(new Node(7, 0, 3, node)); } if (node.getB() < 7 & node.getA() + node.getB() >= 7) { workingList.add(new Node(3 - node.getC(), 7, node.getC(), node)); } if (node.getB() < 7 && node.getB() + node.getC() >= 7) { workingList.add(new Node(node.getA(), 7, 3 - node.getA(), node)); } if (node.getC() > 0 && node.getA() + node.getC() < 10) { workingList.add(new Node(node.getA() + node.getC(), node.getB(), 0, node)); } if (node.getC() > 0 && node.getB() + node.getC() <= 7) { workingList.add(new Node(node.getA(), node.getB() + node.getC(), 0, node)); } if (node.getC() > 0 && node.getB() + node.getC() > 7) { workingList.add(new Node(3, 7, 0, node)); } if (node.getC() < 3 && node.getA() + node.getC() >= 3) { workingList.add(new Node(7 - node.getB(), node.getB(), 3, node)); } if (node.getC() < 3 && node.getB() + node.getC() >= 3) { workingList.add(new Node(node.getA(), 7 - node.getA(), 3, node)); } } } public static void main(String[] args) { Main oilQuestion = new Main(); //启动 oilQuestion.solve(); } } node类 public class Node { private int A; private int B; private int C; private Node parent = null; public Node(int A, int B, int C) { //构造函数 this.A = A; this.B = B; this.C = C; } public Node(int A, int B, int C, Node parent) { //构造函数 this.A = A; this.B = B; this.C = C; this.parent = parent; } @Override public int hashCode() { //与set配套使用,定义一种规则 return A * 100 + B * 10 + C; } @Override public boolean equals(Object obj) { //复写equal,判断是否相等 if (obj instanceof Node) { if (A == ((Node) obj).A && B == ((Node) obj).B && C == ((Node) obj).C) return true; return false; } else { return false; } } @Override //便于输出node public String toString() { StringBuilder stringBuilder = new StringBuilder(); return stringBuilder.append("(").append(A).append(", ").append(B).append(", ") .append(C).append(")").toString(); } public int getA() { return A; } public void setA(int a) { A = a; } public int getB() { return B; } public void setB(int b) { B = b; } public int getC() { return C; } public void setC(int c) { C = c; } public Node getParent() { return parent; } public void setParent(Node parent) { this.parent = parent; } }