更新-主な問題は以下の返信者によって解決されました(「==」があるはずの場所に「=」がありました)が、間違った出力が表示されます。提案されているように、いくつかの入力/出力の例を提供すると思いました。そうすれば、誰かが私が間違っているところを見つけることができるかもしれません。
入力を入力するとします。
......
......
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;
}