分油问题

问题描述:
两个小孩去打油,一人带了 一个一斤的空瓶,另一个带了一个七两、一个三 两的空瓶。原计划各打一斤油,可是由于所带的 钱不够,只好两人合打了一斤油,在回家的路 上,两人想平分这一斤油,可是又没有其它工 具。试仅用三个瓶子(一斤、七两、三两)精确 地分出两个半斤油来。

问题的简化:该问题可简化成如何只使用7两和3两的瓶子,装出5两的油。
用向量(X,Y)表示状态,X表示7两Y表示3两瓶中的油量
问题的起始状态(0,0)
问题的目标状态(5,0)

解释:无论初始状态怎么样,我们总能将所有的油倒进1斤的油瓶,不使用到7两和3两的瓶子。无论目标状态是(5,0)(4,1)(3,2)(2,3)中的任何一个,我们总能将3两的瓶子中的油倒进7两瓶子中,因此,我们将问题的起始和目标状态简化如上。

该问题包含的规则如下:
这里写图片描述

问题的求解:
采用广度优先搜索的方式,当访问到一个节点的时候利用上面8条规则算出子节点的状态。
若子节点已经被生成过了,则不再重复生成该节点(很容易理解,多生成节点就要多遍历,计算量增大)。
为了最终能够回溯产生问题的解,每个子节点都需要记录其父节点的状态。

以下直接贴出自己实验的代码,代码风格不是很好 T-T

struct OilStateNode {
    int lecythus7;//7两瓶中油量
    int lecythus3;//3两瓶中油量
    int state;//油瓶状态 计算方法为7两瓶中油量*10+3两瓶中油量
    int parentState;//当前状态的父节点状态
};
// 分油问题.cpp: 定义控制台应用程序的入口点。
//

map<int, int>visited;//存放已经生成过的点,避免重复生成、访问带来的计算

queue<OilStateNode> oilNodes;//用于存放未访问过的点

OilStateNode InitLecythusTree();//初始化树根

/*
生成给定节点的子节点
返回值<0表示生成的子节点中不包含终止条件
>0表示已经找到终止状态
*/
int Generate(OilStateNode oldState);

/*
回溯产生当前状态的路径
*/
void back(int flag);

/*
给定状态值,计算当前节点的状态(7两瓶中油量及3两瓶中油量)
lecythus7 = flag / 10;
lecythus3 = flag % 10;
返回值:节点
*/
OilStateNode splitState2Node(int flag);
/*
计算方法为7两瓶中油量*10+3两瓶中油量
*/
int CalState(OilStateNode src);

/*
判断当前结点是否为终止节点(7两瓶中油量+3两瓶中油量=5)
返回值为1表示找到终止条件,0表示未终止
*/
int isFinish(OilStateNode src);
OilStateNode getNewState(int lt7, int lt3, int parent);
int main()
{
    OilStateNode root = InitLecythusTree();
    oilNodes.push(root);
    visited.insert(pair<int, int>(root.state, root.parentState));
    while (!oilNodes.empty()) {
        OilStateNode node = oilNodes.front();
        oilNodes.pop();
        int flag = Generate(node);
        if (flag>0) {//到达终止条件时跳出循环
            back(flag);
            break;
        }
    }
    return 0;
}
OilStateNode InitLecythusTree() {
    OilStateNode noState;
    //在该程序中,我们认为(7,3)这种状态跟初始状态(0,0)是一样的,所以在一开始我们设定这个状态不能访问
    noState.lecythus3 = 3;
    noState.lecythus7 = 7;
    noState.state = CalState(noState);
    noState.parentState = -1;
    visited.insert(pair<int, int>(noState.state, noState.parentState));
    OilStateNode root;
    root.lecythus3 = 0;
    root.lecythus7 = 0;
    root.state = CalState(root);
    root.parentState = -1;
    return root;
}
int Generate(OilStateNode oldState) {
    //7两瓶不装满时装满
    if (oldState.lecythus7 < 7) {//这种条件不可能分油成功
        OilStateNode newState= getNewState(7, oldState.lecythus3, oldState.state);
        if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
            oilNodes.push(newState);
            visited.insert(pair<int, int>(newState.state, newState.parentState));
        }
    }
    //3两瓶不装满时装满
    if (oldState.lecythus3 < 3) {
        OilStateNode newState = getNewState(oldState.lecythus7, 3, oldState.state);
        if (isFinish(newState) == 1) {//分油成功
            visited.insert(pair<int, int>(newState.state, newState.parentState));
            return newState.state;
        }
        else
        {
            if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
                oilNodes.push(newState);
                visited.insert(pair<int, int>(newState.state, newState.parentState));
            }
        }
    }
    //7两瓶不空时全部倒回1斤瓶
    if (oldState.lecythus7 > 0) {//这种条件不可能分油成功
        OilStateNode newState = getNewState(0, oldState.lecythus3, oldState.state);
        if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
            oilNodes.push(newState);
            visited.insert(pair<int, int>(newState.state, newState.parentState));
        }
    }

    //3两瓶不空时全部倒回1斤瓶
    if (oldState.lecythus3 > 0) {//这种条件不可能分油成功
        OilStateNode newState = getNewState( oldState.lecythus7, 0,oldState.state);
        if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
            oilNodes.push(newState);
            visited.insert(pair<int, int>(newState.state, newState.parentState));
        }
    }

    //7两瓶中的油全部倒入3两瓶
    if (oldState.lecythus3 + oldState.lecythus7 <= 3) {
        OilStateNode newState = getNewState(0, oldState.lecythus3 + oldState.lecythus7, oldState.state);
        if (isFinish(newState) == 1) {//分油成功
            visited.insert(pair<int, int>(newState.state, newState.parentState));
            return newState.state;
        }
        else
        {
            if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
                oilNodes.push(newState);
                visited.insert(pair<int, int>(newState.state, newState.parentState));
            }
        }
    }

    //3两瓶中的油全部倒入7两瓶
    if (oldState.lecythus3 + oldState.lecythus7 <= 7) {
        OilStateNode newState = getNewState( oldState.lecythus3 + oldState.lecythus7,0, oldState.state);
        if (isFinish(newState) == 1) {//分油成功
            visited.insert(pair<int, int>(newState.state, newState.parentState));
            return newState.state;
        }
        else
        {
            if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
                oilNodes.push(newState);
                visited.insert(pair<int, int>(newState.state, newState.parentState));
            }
        }
    }

    //用3两瓶中的油装满7两瓶
    if (oldState.lecythus3 + oldState.lecythus7 > 7) {//这种条件不可能分油成功
        OilStateNode newState = getNewState(7, oldState.lecythus3 + oldState.lecythus7 - 7, oldState.state);
            if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
                oilNodes.push(newState);
                visited.insert(pair<int, int>(newState.state, newState.parentState));
            }
    }
    //用7两瓶中的油装满3两瓶
    if (oldState.lecythus3 + oldState.lecythus7 > 3) {//这种条件不可能分油成功
        OilStateNode newState = getNewState(oldState.lecythus3 + oldState.lecythus7 -3,3, oldState.state);
        if (isFinish(newState) == 1) {//分油成功
            visited.insert(pair<int, int>(newState.state, newState.parentState));
            return newState.state;
        }
        else
        {
            if (visited.find(newState.state) == visited.end()) {//该状态未被生成过,可以生成
                oilNodes.push(newState);
                visited.insert(pair<int, int>(newState.state, newState.parentState));
            }
        }
    }
    return -1;
}
int CalState(OilStateNode src) {
    return src.lecythus7 * 10 + src.lecythus3;
}
int isFinish(OilStateNode src) {
    if (src.lecythus7 + src.lecythus3 == 5) {
        return 1;
    }
    else {
        return 0;
    }
}
void back(int flag) {
    deque<OilStateNode> res;
    OilStateNode last = splitState2Node(flag);
    res.push_front(last);
    map<int, int>::const_iterator it;
    while (flag != 0) {
        it = visited.find(flag);
        flag = it->second;
        OilStateNode pre = splitState2Node(flag);
        res.push_front(pre);
    }   
    while (!res.empty()) {
        OilStateNode n = res.front();
        res.pop_front();
        cout << "(" << n.lecythus7 << "," << n.lecythus3 << ")" << endl;
    }
}
OilStateNode splitState2Node(int flag) {
    OilStateNode node;
    node.lecythus7 = flag / 10;
    node.lecythus3 = flag % 10;
    return node;
}
OilStateNode getNewState(int lt7,int lt3,int parent) {
    OilStateNode newState;
    newState.lecythus3 = lt3;
    newState.lecythus7 = lt7;
    newState.parentState = parent;
    newState.state = CalState(newState);
    return newState;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值