Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 迭代深化深度优先搜索启发式算法求解“;3传教士和食人族”;挑战_C++_Depth First Search_Std Pair - Fatal编程技术网

C++ 迭代深化深度优先搜索启发式算法求解“;3传教士和食人族”;挑战

C++ 迭代深化深度优先搜索启发式算法求解“;3传教士和食人族”;挑战,c++,depth-first-search,std-pair,C++,Depth First Search,Std Pair,我使用迭代深化深度优先搜索算法实现了它。我的状态由一个三元素向量表示,其中a表示船的一侧(0/1),B和C表示河岸左侧食人族和传教士的数量。 我试图解决这个问题,搜索不同的来源,但我找不到错误 #include <bits/stdc++.h> using namespace std; /* SOLUTION TO 3 CANNIBAL and 3 MISSIONARIES PROBLEM USING ITERATIVE DEEPENING DEPTH FIRST SEARCH

我使用迭代深化深度优先搜索算法实现了它。我的状态由一个三元素向量表示,其中a表示船的一侧(0/1),B和C表示河岸左侧食人族和传教士的数量。 我试图解决这个问题,搜索不同的来源,但我找不到错误


#include <bits/stdc++.h>

using namespace std;
/*
SOLUTION TO 3 CANNIBAL and 3 MISSIONARIES PROBLEM 
USING ITERATIVE DEEPENING DEPTH FIRST SEARCH
https://en.wikipedia.org/wiki/Missionaries_and_cannibals_problem
*/

//state initial state (0,3,3) Side Of the Boat 0->Right/1 /Cannibal/Missionary on right hand side
vector<pair<int, pair<int, int>>> newStatesSet(pair<int, pair<int, int>> state)
{
    //generating all possible states from a given initial state
    vector<pair<int, pair<int, int>>> newStatesSet;
    int C = state.second.first, M = state.second.second;

    if (state.first == 0)
    {
        if (C >= 1)
            newStatesSet.push_back(make_pair(1, make_pair(C - 1, M)));
        if (M >= 1)
            newStatesSet.push_back(make_pair(1, make_pair(C, M - 1)));
        if (C >= 2)
            newStatesSet.push_back(make_pair(1, make_pair(C - 2, M)));
        if (C >= 1 && M >= 1)
            newStatesSet.push_back(make_pair(1, make_pair(C - 1, M - 1)));
        if (M >= 2)
            newStatesSet.push_back(make_pair(1, make_pair(C, M - 2)));
    }
    else
    {
        C = 3 - C;
        M = 3 - M;

        if (C >= 1)
            newStatesSet.push_back(make_pair(0, make_pair(3 - C + 1, 3 - M)));
        if (M >= 1)
            newStatesSet.push_back(make_pair(0, make_pair(3 - C, 3 - M + 1)));
        if (C >= 2)
            newStatesSet.push_back(make_pair(0, make_pair(3 - C + 2, 3 - M)));
        if (C >= 1 && M >= 1)
            newStatesSet.push_back(make_pair(0, make_pair(3 - C + 1, 3 - M + 1)));
        if (M >= 2)
            newStatesSet.push_back(make_pair(0, make_pair(3 - C, 3 - M + 2)));
    }
    return newStatesSet;
}

bool validState(pair<int, pair<int, int>> state)
{
    int C = state.second.first, M = state.second.second;
    if (C > M && M != 0)
        return false;
    C = 3 - C, M = 3 - M;
    if (C > M && M != 0)
        return false;
    return true;
}

//source node , goal node , depth , answer if found is stored here
bool depthLimitedSearch(pair<int, pair<int, int>> source, pair<int, pair<int, int>> goal, int depth, vector<pair<int, pair<int, int>>> &ans)
{

    if (source == goal)
        return true;

    if (depth <= 0)
        return false;

    vector<pair<int, pair<int, int>>> leafs = newStatesSet(source);
    for (int i = 0; i < leafs.size(); i++)
        if (validState(leafs[i]))
        {
            if (depthLimitedSearch(leafs[i], goal, depth - 1, ans))
            {
                ans.push_back(leafs[i]);
                return true;
            }
        }
}

string form(int stateNos, string character)
{
    string rep = "";
    for (int i = 1; i <= 3; i++)
        if (stateNos >= i)
            rep.append(character);
        else
            rep.append(" ");
    return rep;
}

void displayState(pair<int, pair<int, int>> state)
{
    string rep = "";
    rep.append(form(state.second.first, "C"));
    rep.append(form(state.second.second, "M"));
    for (int i = 1; i <= 6; i++)
    {
        if (state.first == 0 && i == 2)
            rep.append("B");
        else if (state.first == 1 && i == 5)
            rep.append("B");
        else
            rep.append(" ");
    }
    rep.append(form(3 - state.second.first, "C"));
    rep.append(form(3 - state.second.second, "M"));
    cout << rep << endl;
}

void iterativeDeepeningSearch(pair<int, pair<int, int>> source, pair<int, pair<int, int>> goal)
{

    vector<pair<int, pair<int, int>>> ans;
    for (int i = 1; i <= 25; i++)
    {
        vector<pair<int, pair<int, int>>> vec;
        depthLimitedSearch(source, goal, i, vec);
        if (vec.size() > 0)
        {
            ans = vec;
            break;
        }
    }
    cout << "C and M represents cannibals and Missionaries \n respectively and B represents the location of boat\n";
    displayState(source);
    for (int i = ans.size() - 1; i >= 0; i--)
        displayState(ans[i]);
}

int main()
{
    pair<int, pair<int, int>> source = make_pair(0, make_pair(3, 3));
    pair<int, pair<int, int>> goal = make_pair(1, make_pair(0, 0));
    iterativeDeepeningSearch(source, goal);
}
结果应该是:

CCCMMM B          
C  MMM    B CC    
CC MMM B    C     
   MMM    B CCC   
C  MMM B    CC    
C  M      B CC MM 
CC MM  B    C  M  
CC        B C  MMM
CCC    B       MMM
C         B CC MMM
CC     B    C  MMM
          B CCCMMM
我搜索了不同的来源,这不是我需要的。
这是我第一次使用向量和对

这行代码看起来有点奇怪
C=3-C,M=3-M。中间的<代码>,<代码>应该是<代码>;<代码>?你使用调试器吗?这是我第一次使用向量和配对——你学习了如何使用C++的非正统方法。你通常会从使用向量和配对的简单程序开始,这样你就知道如何使用它们,而不是编写试图解决具有挑战性的算法问题的程序;没有问题。请使用调试器并确定程序与您的计划有何分歧。
CCCMMM B          
C  MMM    B CC    
CC MMM B    C     
   MMM    B CCC   
C  MMM B    CC    
C  M      B CC MM 
CC MM  B    C  M  
CC        B C  MMM
CCC    B       MMM
C         B CC MMM
CC     B    C  MMM
          B CCCMMM