在简单的高峰时间解算器中使用BFS - 为什么我的代码无法解决问题?

时间:2012-12-09 18:37:19

标签: c++ breadth-first-search

更新 - 主要问题是由下面的回复者解决的(我有一个'='我应该有一个'=='),但现在我得到了错误的输出。我想我会按照建议提供一些输入/输出示例,所以也许有人可以找出我出错的地方。

说我输入输入:

......

......

xx....

......

......

......

这应该产生输出:

4

x r

x r

x r

x r

表示x在4次移动中到达第3行的最右侧位置,x必须在每次移动时向右移动。

相反,我得到输出:

3

x r

x l

x r

在下面的代码中,程序接受一个6x6的字符块来表示游戏板。 (句号是空格,字符代表汽车。)在董事会班级和主要功能中完成的工作是正确的,因为我已经与我的教授核实了。但是,我正在尝试纠正我的输出。我知道我在解决函数中使用的逻辑并不是最好的方法,但是我必须尝试使其工作,这样我就可以在不复制教授解决方案的情况下获得积分。

#include <iostream>
#include <string>
#include <queue>
#include <set>
#include <list>

using namespace std;

class board
{
public:
board() {count = 0;}

bool isSolved()
{
    return currState[17] = 'x';
}

string currState;
string moveList;
int count;
};

bool operator < (const board& board1, const board& board2)
{
return board1.currState < board2.currState;
}

void solve (const board& curr, int i, list<board>& toTest, const set<board>& testedSet, bool vert)
{
board newMove(curr);

if (vert == false)
{
if (i % 6 == 0 && curr.currState[i] != '.')
{
    if (curr.currState[i] == curr.currState[i+1] && curr.currState[i+2] == '.')
    {
        newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
        newMove.currState[i+2] = newMove.currState[i];
        newMove.currState[i] = '.';
        newMove.count++;
    }

    if (curr.currState[i] == curr.currState[i+1] && curr.currState[i] == curr.currState[i+2] && curr.currState[i+3] == '.')
    {
        newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
        newMove.currState[i+3] = newMove.currState[i];
        newMove.currState[i] = '.';     
        newMove.count++;
    }
}

else if ((i + 1) % 6 == 0 && curr.currState[i] != '.')
{
    if (curr.currState[i] == curr.currState[i-1] && curr.currState[i-2] == '.')
    {
        newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
        newMove.currState[i-2] = newMove.currState[i];
        newMove.currState[i] = '.';
        newMove.count++;
    }

    if (curr.currState[i] == curr.currState[i-1] && curr.currState[i] == curr.currState[i-2] && curr.currState[i-3] == '.')
    {
        newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
        newMove.currState[i-3] = newMove.currState[i];
        newMove.currState[i] = '.';
        newMove.count++;
    }
}

else
{
    if (i % 2 != 0 && i % 3 != 0 && curr.currState[i] != '.')
    {
        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i-1] == '.' && curr.currState[i+2] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
            newMove.currState[i-1] = newMove.currState[i];
            newMove.currState[i+1] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i+2] == '.' && curr.currState[i-1] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
            newMove.currState[i+2] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i] == curr.currState[i+2] && curr.currState[i-1] == '.' && curr.currState[i+3] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
            newMove.currState[i-1] = newMove.currState[i];
            newMove.currState[i+2] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i] == curr.currState[i+2] && curr.currState[i+3] == '.' && curr.currState[i-1] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
            newMove.currState[i+3] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }
    }

    if (i % 2 == 0 && (i + 2) % 6 != 0 && curr.currState[i] != '.')
    {

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i-1] == '.' && curr.currState[i+2] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
            newMove.currState[i-1] = newMove.currState[i];
            newMove.currState[i+1] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i+2] == '.' && curr.currState[i-1] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
            newMove.currState[i+2] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i] == curr.currState[i+2] && curr.currState[i+3] == '.' && curr.currState[i-1] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
            newMove.currState[i+3] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }
    }

    if (i % 3 == 0 && curr.currState[i] != '.')
    {
        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i-1] == '.' && curr.currState[i+2] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "l" + "\n";
            newMove.currState[i-1] = newMove.currState[i];
            newMove.currState[i+1] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+1] && curr.currState[i+2] == '.' && curr.currState[i-1] != curr.currState[i])
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "r" + "\n";
            newMove.currState[i+2] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }
    }
}
}

if (vert == true)
{
    if (i < 17)
    {
        if (curr.currState[i] == curr.currState[i+6] && curr.currState[i] == curr.currState[i+12] && curr.currState[i+18] == '.')
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "d" + "\n";
            newMove.currState[i+18] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i+6] && curr.currState[i+12] == '.')
        {
            if (i < 6 || curr.currState[i] != curr.currState[i-6])
            {
                newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "d" + "\n";
                newMove.currState[i+12] = newMove.currState[i];
                newMove.currState[i] = '.';
                newMove.count++;
            }
        }
    }

    if (i > 17)
    {
        if (curr.currState[i] == curr.currState[i-6] && curr.currState[i] == curr.currState[i-12] && curr.currState[i-18] == '.')
        {
            newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "u" + "\n";
            newMove.currState[i-18] = newMove.currState[i];
            newMove.currState[i] = '.';
            newMove.count++;
        }

        if (curr.currState[i] == curr.currState[i-6] && curr.currState[i-12] == '.')
        {
            if (i > 29 || curr.currState[i] != curr.currState[i+6])
            {
                newMove.moveList = newMove.moveList + newMove.currState[i] + " " + "u" + "\n";
                newMove.currState[i-12] = newMove.currState[i];
                newMove.currState[i] = '.';
                newMove.count++;
            }
        }
    }



}

if (testedSet.find(newMove) == testedSet.end())
    toTest.push_back(newMove);
}

int main()
{
list<board> toBeTested;
string input;
board current;
set<board> tested;
bool vertical = false;

for (int i = 0; i < 6; i++)
{
    getline(cin, input);
    current.currState += input;
}

toBeTested.push_back(current);

while (toBeTested.size() > 0 && current.isSolved() == false)
{
    current = toBeTested.front();
    toBeTested.pop_front();

    if (current.isSolved() == false && tested.find(current) == tested.end())
    {
        tested.insert(current);

        for (int i = 0; i < 36; i++)
        {
            solve(current, i, toBeTested, tested, vertical);
            vertical = true;
            solve(current, i, toBeTested, tested, vertical);
            vertical = false;
        }

    }
}

if (current.isSolved() == false)
    cout << current.count << endl << current.moveList;
else
    cout << -1 << endl;

return 0;
}

1 个答案:

答案 0 :(得分:0)

很难弄清楚你在这里要做什么,因为很多值都是硬编码的,并且程序中没有使用很多抽象。

我猜你正在尝试做http://www.theiling.de/projects/rushhour.html之类的事情,输入就是这样的板子:

aaobcc
..ob..
xxo...
deeffp
d..k.p
hh.k.p

如果您从我的评论中对board::isSolved()进行了更正:

bool isSolved()
{
    return currState[17] == 'x';
}

你至少会获得-1以外的东西:

10
b d
c l
h r
p u
p u
p u
f r
k u
h l
h r

但是,这不是输入问题的解决方案。

看起来你没有正确计算下一个状态。在车辆移动后,应该有一个循环来迭代车辆的每个部分。

绝对尝试抽象出更多细节。例如,您可以向board添加方法,以便(1)计算有效移动列表,以及(2)应用单个移动,返回新的board。此外,如果您还没有学习如何使用gdb之类的调试器,那么现在是开始学习的绝佳时机。见How do I use the MinGW gdb debugger to debug a C++ program in Windows?