すでにかなりのJavaコーディングを行っていますが、私はまったく新しいC++です。今は自分のコードで何が起こっているのか分かりません。このコードでは、マップ標準ライブラリでコンパイルエラーが発生します。それはCannot increment value of type ' std::_1::pari<int, int>'
と言い、map
、insert(_InputIterator __f, _InputIterator __l)
で起こっています。コードをコンパイルするときの標準ライブラリのコンパイルエラー
私はstackoverflowコミュニティは一般的に他の人々の宿題を解決するのが好きではないことを知っていますが、私はこれを実装するための真の試みをしたと思うし、それ以外にも何が起こっているのか非常に興味があります。
typedef std::pair<int, int> location;
std::set<location> neighbours(location loc, std::set<std::pair<location, location>> labyrinth, int& size) {
std::set<location> neighbours;
location locFmin = location(loc.first - 1, loc.second);
location locSmin = location(loc.first, loc.second - 1);
location locFplus = location(loc.first + 1, loc.second);
location locSplus = location(loc.first, loc.second + 1);
if (loc.first - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locFmin)) == labyrinth.end()) {
neighbours.insert(locFmin);
}
if (loc.second - 1 >= 0 && labyrinth.find(std::pair<location, location>(loc, locSmin)) == labyrinth.end()) {
neighbours.insert(locSmin);
}
if (loc.first + 1 < size && labyrinth.find(std::pair<location, location>(loc, locFplus)) == labyrinth.end()) {
neighbours.insert(locFplus);
}
if (loc.second + 1 < size && labyrinth.find(std::pair<location, location>(loc, locSplus)) == labyrinth.end()) {
neighbours.insert(locSplus);
}
return neighbours;
}
int Labyrinth(std::set<std::pair<location, location>> labyrinth, int size) {
std::map<location, location> forest;
std::set<location> level;
std::set<location> known;
known.insert(location(0,0));
level.insert(location(0,0));
while (!level.empty()) {
std::set<location> nextLevel;
for (location loc: level) {
for (location neighbour: neighbours(loc, labyrinth, size)) {
if (known.find(neighbour) != known.end()) {
known.insert(neighbour);
forest.insert(neighbour, loc);
nextLevel.insert(neighbour);
}
}
}
level = nextLevel;
}
std::list<location> path;
location walk = location(size - 1, size - 1);
path.push_front(walk);
while (walk != location(0, 0)) {
walk = forest[walk];
path.push_front(walk);
}
int answ = path.size();
return answ;
}
これは、幅優先探索はもちろんサイズ*サイズlocation(x, y)
オブジェクトのサイズとsize
の正方形の迷路の谷行う必要があるアルゴリズムです。 入ってくるリストlabyrinth
は迷路の壁を定義しています。最終的に関数は(0、0)から(size - 1、size - 1)までの最短経路に含まれるノードの数を返すべきです。誰もがこの問題を解決するために、より良いアイデアを思い付くために開始する前に
これはアルゴリズム
std::set<std::pair<location, location> > labyrinth;
labyrinth.insert(std::pair<location, location>(location(0, 0), location(1, 0)));
labyrinth.insert(std::pair<location, location>(location(0, 1), location(1, 1)));
labyrinth.insert(std::pair<location, location>(location(0, 2), location(0, 3)));
labyrinth.insert(std::pair<location, location>(location(1, 1), location(1, 2)));
labyrinth.insert(std::pair<location, location>(location(1, 2), location(2, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 3), location(3, 3)));
labyrinth.insert(std::pair<location, location>(location(2, 2), location(3, 2)));
labyrinth.insert(std::pair<location, location>(location(2, 1), location(3, 1)));
int labAnswer = Labyrinth(labyrinth, 4);
std::cout << labAnswer << std::endl;
if (labAnswer == 13)
{
std::cout << "Correct" << std::endl;
}
else
{
std::cout << "Incorrect" << std::endl;
}
ための簡単なテストです。私はアルゴリズムに関する本のbfs java実装のグラフからbfsコードのアイディアを得ました。私はこのパズルをより効率的に解決することに興味がなく、常に何かをするより良い方法があるでしょう。私は自分のコードで何が起こっているのか、そしておそらくここでは何がC++であるのかを知りたいと思います。
は、コンパイルエラーを投稿する必要があります...あなたができない可能性がありますそれを解釈することができますが、私たちの何人かができます。 –
ああ、そうは思わなかった。エラーを含めるように質問を編集し、エラーを表示する部分を編集しました。 – Fr4nc3sc0NL
完全なコンパイル出力? –