Kettle problem - the problem of blind search

A small artificial intelligence exercise class, you can search directly DFS violence.

BACKGROUND problem: Given two bottle, a package (4) gallons of water, can hold a 3 gallons of water. No measure marks on the kettle. There is a water tap and from the available pot irrigation. The question is how can hold four gallons of kettle just installed only two gallons of water.

Solving this problem is given in the textbook. Here are just a given program implementation.

// definition of kettle issues of several operations:

// 0. No operation

// when 1.4 gallons to fill pot dissatisfied

// when 2.3 gallons to fill pot dissatisfied

// 3 to 4 gallons Empty the water in the kettle

// 4 to 3 gallons Empty the water in the kettle

// 5 to 3 gallons of water in the kettle to pour four gallons kettle, kettle full until four gallons

6 // Put four gallons of water in the kettle to pour three gallons kettle, kettle full until 3 gallons

// 7. To 3 gallons of water in the kettle all fell to 4 gallons kettle

// 8. To 4 gallons of water in the kettle all fell to 3 gallons kettle

// solve basic idea is to solve the state space law Let X 4 gallons of water kettle, Y is 3 gallons of water kettle.

// all possible (X, Y) as the sequence of the set of states state space M, the operation {1, 2, 3, 4, 5, 6, 7, 8} is a set of input characters.

// start state (0,0), the termination state is {(2, Y)}, Y may be 0,1,2,3.

// When an input string is the language of M, the string is a possible solution of the problem kettle.

// programming considerations are: Water [5] [4] identifies an array, is 1, indicating that the state has been reached, expressed as 0 does not reach this state.

Screenshot operating results:
Here Insert Picture Description

java code:

public class 水壶问题 {
    static int maxX=4,maxY=3;
    static int[][] water=new int[maxX+1][maxY+1];
    static LinkedList<Output> path=new LinkedList<>();
    static int resCount=1;
    public static void main(String[] args) {
        jiashui(0,0);
    }
    public static void jiashui(int x,int y){
        if (x==2){
            System.out.println("第"+(resCount++)+"种解决方案:");
            System.out.println("4加仑水壶\t3加仑水壶\t所使用的操作");
            path.forEach(System.out::println);
            return;
        }
        if (water[0][0]==0){
            water[0][0]=1;
            path.add(new Output(0,0,0));
            jiashui(0,0);
            path.removeLast();
            water[0][0]=0;
        }
        if (x<maxX && water[maxX][y]==0){
            water[maxX][y]=1;
            path.add(new Output(maxX,y,1));
            jiashui(maxX,y);
            path.removeLast();
            water[maxX][y]=0;
        }
        if (y<maxY && water[x][maxY]==0){
            water[x][maxY]=1;
            path.add(new Output(x,maxY,2));
            jiashui(x,maxY);
            path.removeLast();
            water[x][maxY]=0;
        }
        if (x>0 && water[0][y]==0){
            water[0][y]=1;
            path.add(new Output(0,y,3));
            jiashui(0,y);
            path.removeLast();
            water[0][y]=0;
        }
        if (y>0 && water[x][0]==0){
            water[x][0]=1;
            path.add(new Output(x,0,4));
            jiashui(x,0);
            path.removeLast();
            water[x][0]=0;
        }
        if (x+y>=maxX &&water[maxX][x+y-maxX]==0){
            water[maxX][x+y-maxX]=1;
            path.add(new Output(maxX,x+y-maxX,5));
            jiashui(maxX,x+y-maxX);
            path.removeLast();
            water[maxX][x+y-maxX]=0;
        }
        if (x+y>=maxY &&water[x+y-maxY][maxY]==0){
            water[x+y-maxY][maxY]=1;
            path.add(new Output(x+y-maxY,maxY,6));
            jiashui(x+y-maxY,maxY);
            path.removeLast();
            water[x+y-maxY][maxY]=0;
        }
        if(x+y<=maxX && y>0&& water[x+y][0]==0){
            water[x+y][0]=1;
            path.add(new Output(x+y,0,7));
            jiashui(x+y,0);
            path.removeLast();
            water[x+y][0]=0;
        }
        if(x+y<=maxY && x>0&&water[0][x+y]==0){
            water[0][x+y]=1;
            path.add(new Output(0,x+y,8));
            jiashui(0,x+y);
            path.removeLast();
            water[0][x+y]=0;
        }


    }
    private static class Output{
        int x;
        int y;
        int opt;
        public Output(int x,int y,int opt){
            this.x=x;
            this.y=y;
            this.opt=opt;
        }

        @Override
        public String toString() {
            return "Output{" +
                    "x=" + x +
                    ", y=" + y +
                    ", opt=" + opt +
                    '}';
        }
    }
}
Released seven original articles · won praise 0 · Views 292

Guess you like

Origin blog.csdn.net/zwn888zwn/article/details/104840781