在简单的 Rush Hour 求解器中使用 BFS - 为啥我的代码不能求解电路板?
Posted
技术标签:
【中文标题】在简单的 Rush Hour 求解器中使用 BFS - 为啥我的代码不能求解电路板?【英文标题】:Using BFS in a simple Rush Hour solver - why is my code not solving the board?在简单的 Rush Hour 求解器中使用 BFS - 为什么我的代码不能求解电路板? 【发布时间】:2012-12-09 18:37:18 【问题描述】:更新 - 主要问题已由下面的回复者解决(我有一个 '=' 我应该有一个 '=='),但现在我得到了错误的输出。我想我会按照建议提供一些输入/输出示例,这样也许有人可以发现我哪里出错了。
假设我输入了输入:
......
......
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;
【问题讨论】:
return currState[17] = 'x';
应该是return currState[17] == 'x';
如果您能提供一些示例输入和输出将会有所帮助。
好的。是的。这帮助很大。哈哈。不敢相信我是个大白痴,但我想有人发现这样的错误总是有帮助的。谢谢你。当然,这解决了 -1 问题,但现在我得到了错误的答案。例如,当我进入第三行前面只有两个 x 的网格时,输出应为“4”(需要 4 步才能将 x 移到点 17)和“x r x r x r x r”。相反,它读取 3 个动作,并且 "x r x l x r"
【参考方案1】:
在这里很难弄清楚您要做什么,因为许多值是硬编码的,并且程序中没有使用很多抽象。
我猜你正在尝试做类似http://www.theiling.de/projects/rushhour.html 的事情,其中输入是一个类似的板:
aaobcc ..ob.. xx... deeffp d..k.p hh.k.p如果您从我的评论中更正board::isSolved()
:
bool isSolved()
return currState[17] == 'x';
你至少会得到-1以外的东西:
10 b d cl 小时 普 普 普 fr 库 升 小时但是,这并不能解决输入问题。
您似乎没有正确计算下一个状态。移动车辆后,应该有一个循环遍历车辆的每一部分。
一定要尝试抽象更多细节。例如,也许您可以向board
添加方法,用于(1)计算有效移动列表和(2)应用单个移动,返回一个新的board
。此外,如果您还没有学会如何使用诸如gdb
之类的调试器,那么现在是开始学习的好时机。见How do I use the MinGW gdb debugger to debug a C++ program in Windows?
【讨论】:
你是对的,我为草率的代码道歉。我想我会跟踪求解函数,看看我哪里出错了。谢谢你的好建议。 @TheRationalist:顺便说一下,BerkeleyX(edX 的一部分)有一个关于人工智能的优秀免费课程:CS188.1x: Artificial Intelligence。课程已结束,但您仍然可以注册并学习材料。第一周是搜索算法 BFS、DFS、统一成本和 A*。如果你完成了第一个编程项目,你会看到一个很好的例子,可以抽象出搜索问题中的细节。以上是关于在简单的 Rush Hour 求解器中使用 BFS - 为啥我的代码不能求解电路板?的主要内容,如果未能解决你的问题,请参考以下文章