编译代码

时间:2016-04-28 22:41:04

标签: c++ xcode stdmap std-pair stdset

我已经完成了一些java编码,但我对c ++完全不熟悉,并且不知道我的代码现在正在进行什么。此代码在地图标准库中给出了编译错误。它说:Cannot increment value of type ' std::_1::pari<int, int>',并且发生在mapinsert(_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;
}

这是一种算法,它应该通过大小为size的方形迷宫执行广度优先搜索,当然大小为* location(x, y)个对象。 传入列表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 ++方面。

2 个答案:

答案 0 :(得分:1)

你没有正确使用std :: map和insert,

forest.insert(neighbour, loc);

应该是

forest[neighbor] = loc;

答案 1 :(得分:-2)

检查#include标题,您需要

#include <map>

此外,如果您添加using namespace std;,则可以跳过为每个声明添加std::