野人过河问题

1  问题定义

河的两岸有三个传教士和三个野人需要过河,目前只有一条能装下两个人的船,在河的任何一方或者船上,如果野人的人数大于传教士的人数,那么传教士就会被野人攻击,怎么找出一种安全的渡河方案呢?

2  算法分析

首先,先来看看问题的初始状态和目标状态,定义河的两岸分别为左岸和右岸,设定状态集合为(左岸传教士人数,右岸野人数,右岸传教士人数,右岸野人数,船的位置),船的位置:-1表示船在左岸,1表示船在右岸。

初始状态:(3,3,0,0,0,-1)

目标状态:(0,0,3,3,1)

然后,整个问题就抽象成了怎样从初始状态经中间的一系列状态达到目标状态。问题状态的改变是通过划船渡河来引发的,所以合理的渡河操作就成了通常所说的算符,根据题目要求,可以得出以下5个算符(按照渡船方向的不同,也可以理解为10个算符):

渡1野人、渡1传教士、渡1野人1传教士、渡2野人、渡2传教士

根据船的位置,向左移或向右移通过递归依次执行5种算符,判断是否找到所求,并排除不符合实际的状态,就可以找到所有可能的解,如图1所示为递归函数流程图。

3  源代码

package main;

import java.util.ArrayList;
import java.util.List;


public class River {


    public static void main(String[] args) {
        List lastParameters = new ArrayList();
        List operation = new ArrayList();


        RiverSides currentState = new RiverSides(3, 3, 0, 0, 1);


        lastParameters.add(currentState);


        cvsWdfs(currentState, lastParameters, operation, 0);


        lastParameters.remove(lastParameters.size() - 1);


        System.out.println("执行完毕");


    }


    private static int mycount = 0;// 统计成功过河次数


    public static int cvsWdfs(RiverSides lastState,
                              List lastParameters, List operation,
                              int ifboacurrentStatety) {


        if (lastState.getChurchR() == 3 && lastState.getWildR() == 3) {
            mycount++;
            System.out.println("第" + mycount + "次成功过河");
            System.out.println("传教士 野人 |  移动方向");
            for (int i = 0; i < operation.size(); i++) {
                System.out.println(operation.get(i));
            }
            return 0;
        }
// 判断过河操作否重复,去除死循环
        for (int i = 0; i < lastParameters.size() - 1; i++) {
            if (lastParameters.get(i).getWildL() == lastState.getWildL()
                    && lastParameters.get(i).getChurchL() == lastState
                    .getChurchL()) {


                if (lastState.getBoat() == lastParameters.get(i).getBoat())
                    return 0;
            }
        }
// 检验人数数据合法性
        if (lastState.getChurchL() < 0 || lastState.getWildL() < 0
                || lastState.getChurchR() < 0 || lastState.getWildR() < 0)
            return 0;
// 传教士是否被吃
        if ((lastState.getChurchL() < lastState.getWildL() && lastState .getChurchL() != 0)
                || (lastState.getChurchR() < lastState.getWildR() && lastState.getChurchR() != 0))
            return 0;
// 递归执行五类过河操作,boat=-1船在左岸,boat=1船在右岸,传入boat为上一次船位置
// 下次应当取反
        RiverSides currentState = new RiverSides();


// 两个传教士过河
        if (lastState.getBoat() == 1)
            operation.add("  2     0   | 左岸->右岸");
        else
            operation.add("  2     0   | 右岸->左岸");
        currentState.setChurchL(lastState.getChurchL() - 2
                * lastState.getBoat());
        currentState.setWildL(lastState.getWildL());
        currentState.setChurchR(lastState.getChurchR() + 2
                * lastState.getBoat());
        currentState.setWildR(lastState.getWildR());
        currentState.setBoat(-lastState.getBoat());
        lastParameters.add(currentState);
        cvsWdfs(currentState, lastParameters, operation, 0);
        operation.remove(operation.size() - 1);
        lastParameters.remove(lastParameters.size() - 1);


// 两个野人过河
        if (lastState.getBoat() == 1)
            operation.add("  0     2   | 左岸->右岸");
        else
            operation.add("  0     2   | 右岸->左岸");
        currentState.setChurchL(lastState.getChurchL());
        currentState.setWildL(lastState.getWildL() - 2 * lastState.getBoat());
        currentState.setChurchR(lastState.getChurchR());
        currentState.setWildR(lastState.getWildR() + 2 * lastState.getBoat());
        currentState.setBoat(-lastState.getBoat());
        lastParameters.add(currentState);
        cvsWdfs(currentState, lastParameters, operation, 0);
        operation.remove(operation.size() - 1);
        lastParameters.remove(lastParameters.size() - 1);


// 一个野人,一个传教士
        if (lastState.getBoat() == 1)
            operation.add("  1     1   | 左岸->右岸");
        else
            operation.add("  1     1   | 右岸->左岸");
        currentState.setChurchL(lastState.getChurchL() - 1
                * lastState.getBoat());
        currentState.setWildL(lastState.getWildL() - 1 * lastState.getBoat());
        currentState.setChurchR(lastState.getChurchR() + 1
                * lastState.getBoat());
        currentState.setWildR(lastState.getWildR() + 1 * lastState.getBoat());
        currentState.setBoat(-lastState.getBoat());
        lastParameters.add(currentState);
        cvsWdfs(currentState, lastParameters, operation, 0);
        operation.remove(operation.size() - 1);
        lastParameters.remove(lastParameters.size() - 1);


// 一个传教士过河
        if (lastState.getBoat() == 1)
            operation.add("  1     0   | 左岸->右岸");
        else
            operation.add("  1     0   | 右岸->左岸");
        currentState.setChurchL(lastState.getChurchL() - 1
                * lastState.getBoat());
        currentState.setWildL(lastState.getWildL());
        currentState.setChurchR(lastState.getChurchR() + 1
                * lastState.getBoat());
        currentState.setWildR(lastState.getWildR());
        currentState.setBoat(-lastState.getBoat());
        lastParameters.add(currentState);
        cvsWdfs(currentState, lastParameters, operation, 0);
        operation.remove(operation.size() - 1);
        lastParameters.remove(lastParameters.size() - 1);


// 一个野人过河
        if (lastState.getBoat() == 1)
            operation.add("  0     1   | 左岸->右岸");
        else
            operation.add("  0     1   | 右岸->左岸");
        currentState.setChurchL(lastState.getChurchL());
        currentState.setWildL(lastState.getWildL() - 1 * lastState.getBoat());
        currentState.setChurchR(lastState.getChurchR());
        currentState.setWildR(lastState.getWildR() + 1 * lastState.getBoat());
        currentState.setBoat(-lastState.getBoat());
        lastParameters.add(currentState);
        cvsWdfs(currentState, lastParameters, operation, 0);
        operation.remove(operation.size() - 1);
        lastParameters.remove(lastParameters.size() - 1);
        return 0;
    }


}



//定义过河状态类
class RiverSides {
    private int churchL;
    private int wildL;
    private int churchR;
    private int wildR;
    private int boat;

    public RiverSides() {
    }


    public int getChurchL() {
        return churchL;
    }


    public void setChurchL(int churchL) {
        this.churchL = churchL;
    }


    public int getWildL() {
        return wildL;
    }


    public void setWildL(int wildL) {
        this.wildL = wildL;
    }


    public int getChurchR() {
        return churchR;
    }


    public void setChurchR(int churchR) {
        this.churchR = churchR;
    }


    public int getWildR() {
        return wildR;
    }


    public void setWildR(int wildR) {
        this.wildR = wildR;
    }


    public int getBoat() {
        return boat;
    }


    public void setBoat(int boat) {
        this.boat = boat;
    }


    public RiverSides(int churchL, int wildL, int churchR, int wildR, int boat) {
        super();
        this.churchL = churchL;
        this.wildL = wildL;
        this.churchR = churchR;
        this.wildR = wildR;
        this.boat = boat;
    }
}


转自:https://blog.csdn.net/Joebug/article/details/78302544

你可能感兴趣的:(经典编程题目,Java,野人过河)