深搜算法:倒油/面向对象的思想来做

题目:有一位厨师要从盛12斤油(a桶)的桶中倒出6斤油来,可是手边只有盛8斤油(b桶)和盛5斤油(c桶)的两个桶,问如何操作才能将6斤取出来呢?

下面为JAVA实现代码:
主类:

package cn.hncu.oil.dfs1;

import cn.hncu.oil.common.Bucket;
import cn.hncu.oil.common.DumpCase;
import cn.hncu.oil.common.Myset;

public class DumpOilDFS {
    public static void main(String[] args) {

        Bucket buckets[] = new Bucket[3];
        buckets[0] = new Bucket(12, 12);
        buckets[1] = new Bucket(8, 0);
        buckets[2] = new Bucket(5, 0);

        DumpCase u = new DumpCase(buckets);
        Myset caseset = new Myset();
        caseset.add(u);

        dfs(u,caseset);

    }

    private static void dfs(DumpCase u0, Myset caseset) {

        for(Bucket bucket: u0.getBucket()){
            if(bucket.getNow()==6){
                //System.out.println("find a case");
                print(u0);
                return;
            }
        }

        int n = u0.getBucket().length;//桶的个数
        DumpCase u = new DumpCase(u0);
        //用备份节点去搜
        //遍历所有的DumpCase: 依次让桶i向j倒
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j){//不能自己给自己的桶倒油
                    continue;
                }
                //算出桶i给j倒时,能倒多少-->temp
                int temp = u.getBucket()[i].canOut();
                if(u.getBucket()[j].canIn()<u.getBucket()[i].canOut()){
                    temp = u.getBucket()[j].canIn();
                }
                //倒油
                u.getBucket()[i].out(temp);
                u.getBucket()[j].in(temp);

                //判断该情况是否已经出现过了//如果存在,要还原(把油倒回去)
                if(caseset.contains(u)){
                    u.getBucket()[i].in(temp);
                    u.getBucket()[j].out(temp);
                    continue;
                }

                DumpCase v = new DumpCase(u);
                v.setParent(u0);
                caseset.add(v);
                //System.out.println(a);
                dfs(v,caseset);

                u.getBucket()[i].in(temp);
                u.getBucket()[j].out(temp);

            }
        }

    }

    private static void print(DumpCase u0) {
        Myset set  =new Myset();
        set.add(u0);
        DumpCase v =u0.getParent();
        while(v!=null){
            set.add(v);
            //System.out.println(v.getBucket()[0].getNow()+","+v.getBucket()[1].getNow()+","+v.getBucket()[2].getNow());
            v= v.getParent();
        }
        System.out.println("------------");
        //System.out.println("12,0,0");
        Object objs[] = set.getAll();

        for(int i=objs.length-1;i>=0;i--){
            DumpCase u =(DumpCase) objs[i];
            System.out.println(u.getBucket()[0].getNow()+","+u.getBucket()[1].getNow()+","+u.getBucket()[2].getNow());
        }

    }

}

DumpCase 类:

package cn.hncu.oil.common;

import java.util.Arrays;

public class DumpCase {
    Bucket buckets[];
    DumpCase parent = null;

    public DumpCase(){

    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + Arrays.hashCode(buckets);
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        DumpCase other = (DumpCase) obj;
        if (!Arrays.equals(buckets, other.buckets))
            return false;
        return true;
    }

    public DumpCase(Bucket buckets[]){
        this.buckets = buckets;
    }

    public DumpCase(DumpCase u) {//必须要深拷贝
        this.buckets = new Bucket[u.getBucket().length];
        for(int i=0;i<u.getBucket().length;i++){
            this.buckets[i] = new Bucket(0, 0);
            this.buckets[i].max=u.buckets[i].max;
            this.buckets[i].now=u.buckets[i].now;
        }

    }

    public Bucket[] getBucket() {
        return buckets;
    }

    public void setBucket(Bucket[] buckets) {
        this.buckets = buckets;
    }

    public DumpCase getParent() {
        return parent;
    }

    public void setParent(DumpCase parent) {
        this.parent = parent;
    }

}

Bucket 类:

package cn.hncu.oil.common;

public class Bucket {//桶的容量和现在装的油的多少
    int now;
    int max;

    public Bucket(int max,int now) {
        this.max=max;
        this.now=now;
    }

    public void in(int n){
         now+=n;
    }

    public void out(int n){
        now-=n;
    }

    public int getNow() {
        return now;
    }

    public int getMax() {
        return max;
    }

    public int canIn(){
        return max-now;
    }

    public int canOut(){
        return now;
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + max;
        result = prime * result + now;
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Bucket other = (Bucket) obj;
        if (max != other.max)
            return false;
        if (now != other.now)
            return false;
        return true;
    }

}

Myset 类:

package cn.hncu.oil.common;

public class Myset {
    private Object[] objs = new Object[0];

    public boolean contains(Object obj){
        for(Object objtemp:objs){
            if(objtemp.equals(obj)){
                return true;
            }
        }
        return false;
    }

    public boolean add(Object obj){
        if(contains(obj)){
            return false;
        }
        Object[] objstemp = new Object[objs.length+1];
        System.arraycopy(objs, 0, objstemp, 0, objs.length);
        objstemp[objs.length]=obj;
        objs = objstemp;
        return true;
    }

    public Object[] getAll(){
        return objs;
    }

    public int Size(){
        return objs.length;
    }

}
时间: 2024-10-17 03:20:50

深搜算法:倒油/面向对象的思想来做的相关文章

利用 JSP的思想来做ASP

js 这几天开始接触JSP里面一些BEAN的写法,然后自己想了想,认为其实在ASP里面也可以采取这一思想来做.虽然不是很纯,不彻底,但是能够把一些逻辑处理分离出来,更适合程序的移植性,提高了开发周期.我自己写了个类ConnEX包含了一些对数据库的操作,觉得应该可以包括一大部分的逻辑处理,但是这样也提高了错误几率,如果你把SQL语句控制的比较好的话,应该是利大于弊的,这里都是一点点拙见,望大家指正. 程序的功能有了个大体的框架,其实可以自己添加一些功能,比如开始的数据库连接 ,可以先设置变量然后通

深搜算法实例:老鼠走迷宫(一)

这个是简单的深搜,应该输入深搜中抛砖型的,联系下代码,回顾一下深搜的思想. 本题的要求是,在开始点(1,1)和终点(5,5)放一只老鼠,让老鼠找到一条路径走出去(暂时不考虑最短路径),找到后输出路径. 最简单的想法就是对于上下左右四个进行刨根型的搜索,找到就返回输出,进入死胡同了就原路返回,找最近的有其他路径的点,继续搜索,知道找出为止. 下面是代码部分. #include <stdio.h> #include <stdlib.h> #define SUCCESS 1 #defin

利用JSP的思想来做ASP

js    程序的功能有了个大体的框架,其实可以自己添加一些功能,比如开始的数据库连接 ,可以先设置变量然后通过INIT() 来选择不同类型的数据库 <% 'On Error Resume Next Class ConnEx public ConnEx public DBpath '---------数据库路径 public DBtype '---------数据库类型 1(Access) 2(SqlServer) 3(可扩充) public ConnMethod '--------连接方式 (

穷举搜索:回溯与深搜

计算机常用算法大致有两大类,一类叫蛮力算法,一类叫贪心算法,前者常使用的手段就是搜索,对全部解空间进行地毯式搜索,直到找到指定解或最优解. [建立解空间] 问题的解应该如何描述,如何建立?借助图论的思想,我们可以用图来描述,图的定义为G,由顶点集和边集构成,顶点即实实在在的数 据.对象,而边可以抽象为关系,即顶点间的关系,这种关系不一定非要在数据结构上表现出来,用数据结构的语言来描述,如果关系是一对一,则为线性表,如果 关系是一对多,则为树,如果关系是多对多,则为图,如果完全没有关系,则为集合.

HDOJ/HDU 1015 Safecracker(深搜)

Problem Description === Op tech briefing, 2002/11/02 06:42 CST === "The item is locked in a Klein safe behind a painting in the second-floor library. Klein safes are extremely rare; most of them, along with Klein and his factory, were destroyed in Wo

ZOJ(ZJU) 1002 Fire Net(深搜)

Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to shoot. The fo

acm-邻接矩阵能进行深搜么。现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗

问题描述 邻接矩阵能进行深搜么.现在会邻接表的深搜,还用把邻接矩阵转化为邻接表吗 邻接矩阵如果能进行的话.麻烦大神给点代码啊.小白..如题...... 解决方案 void dfs(int i){ visited[i]=1; for(int j=0;j<n;j++){ if(G[i][j]==1&&visited[j]==0){ dfs(j); } } } 解决方案二: 这是用邻接矩阵求最小生成树的,题主看下能否帮的上忙,如果能帮的上,希望可以采纳 #include #include

c++-UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法

问题描述 UVa1374快速幂运算迭代深搜法 ,C++初学者,求优化方法 这是我的代码,优化过几次,但是还是很慢很慢,求大神给优化途经,就是在我的代码的进行优化 #include #include #include #include using namespace std; bool search(int,set&,int dep,int); int MAX=1; int tempMax; int main() { sets;int n; for(n=2;n!=1000;++n) { s.cle

HDOJ/HDU 1242 Rescue(经典BFS深搜-优先队列)

Problem Description Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison. Angel's friends want to save Angel. Their task is: approa