使用BFS问题在图中找到最短路径

时间:2018-11-22 10:53:54

标签: c++ graph-theory shortest-path breadth-first-search maze

我正在学校里做家庭作业。我必须使用BFS算法找到图中的最短路径。起点是2,0是墙,1是路径,3是终点。我从txt文件中读取了2D矩阵,我需要使用邻接表将该矩阵转换为图形,然后为该图形生成BFS算法。所以我的问题是,当我打印路径时,它仅显示“ 18”。我认为我的BPS算法不会填充resultList,但我只是无法找到原因。请问有人可以帮助我吗?

struct Graf {

int matrix[200][200];
int numberOfLines;
int numberOfColumns;
int intrare;

std::vector<int>iesiri;
std::map<int, std::vector<int>> grafNeor;
std::map<int, std::vector<int>>::iterator it;

void citire() {

    std::ifstream f("Matrix.txt");
    f >> numberOfLines >> numberOfColumns;
    std::cout << "numberOfLines: " << numberOfLines << std::endl;
    std::cout << "numberOfColumns: " << numberOfColumns << std::endl;

    for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++)
        for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
            f >> matrix[lineNumber][columnNumber];

    f.close();

    for (int i = 0; i < numberOfLines; ++i) {
        for (int j = 0; j < numberOfColumns; ++j) {
            if (matrix[i][j] == 3) {
                intrare = numberOfColumns * i + j;
            }
            if (matrix[i][j] == 2) {
                iesiri.push_back(numberOfColumns * i + j);
            }
        }
    }

    for (int lineNumber = 0; lineNumber < numberOfLines; lineNumber++) {
        for (int columnNumber = 0; columnNumber < numberOfColumns; columnNumber++)
            std::cout << matrix[lineNumber][columnNumber] << " ";
        std::cout << std::endl;
    }
}
void tranzitie() {

    for (int i = 0; i < numberOfColumns * numberOfLines; i++)
        grafNeor.insert(std::pair<int, std::vector<int> >(i, std::vector<int>()));

    for (int i = 0; i < numberOfLines; i++) {
        for (int j = 0; j < numberOfColumns; j++) {
            int key = numberOfColumns * i + j;
            if (matrix[i][j] != 0) {
                // stanga
                if (j > 0 && matrix[i][j - 1] != 0) {
                    int currentValue = numberOfColumns * i + j - 1;
                    it = grafNeor.find(key);
                    if (it != grafNeor.end())
                        grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));

                }
                // dreapta
                if (j < numberOfColumns - 1 && matrix[i][j + 1] != 0) {
                    int currentValue = numberOfColumns * i + j + 1;
                    it = grafNeor.find(key);
                    if (it != grafNeor.end())
                        grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
                }
                // sus
                if (i > 0 && matrix[i - 1][j] != 0) {
                    int currentValue = numberOfColumns * (i - 1) + j;
                    it = grafNeor.find(key);
                    if (it != grafNeor.end())
                        grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
                }
                // jos
                if (i < numberOfLines - 1 && matrix[i + 1][j] != 0) {
                    int currentValue = numberOfColumns * (i + 1) + j;
                    it = grafNeor.find(key);
                    if (it != grafNeor.end())
                        grafNeor.insert(std::pair<int, std::vector<int> >(currentValue, std::vector<int>()));
                }
            }
        }
    }
}
void PBF() {

    std::vector<int> noduri;

    for (int i = 0; i < numberOfColumns * numberOfLines; i++)
        noduri.push_back(i);

    std::vector<int> noduriNevizitate;
    noduriNevizitate.assign(noduri.begin(), noduri.end());
    noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), intrare));

    std::vector<int> noduriCurente;
    noduriCurente.push_back(intrare);

    std::vector<int> noduriVizitate;
    std::vector<int> drum;

    for (int i = 0; i < numberOfColumns * numberOfLines; i++)
        drum.push_back(0);

    for (int i = 0; i < noduriNevizitate.size(); i++) {
        drum.assign(noduriNevizitate.at(i), -1);
    }

    int current = 0;

    while (noduriCurente.size() > 0)
    {
        current = noduriCurente.at(0);
        for (int i = 0; i < iesiri.size(); i++) {
            if (iesiri[i] == current)
                break;
        }

        for (int vecin : grafNeor.at(current)) {
            for (int i = 0; i <= noduriNevizitate.size(); i++) {
                if (noduriNevizitate[i] == vecin) {
                    noduriNevizitate.erase(std::remove(noduriNevizitate.begin(), noduriNevizitate.end(), vecin));
                    noduriCurente.push_back(vecin);
                    drum.assign(vecin, current);
                }
            }
        }
        noduriCurente.erase(std::remove(noduriCurente.begin(), noduriCurente.end(), current));
        noduriVizitate.push_back(current);
    }
    int before = current;
    std::vector<int> resultList;

    while (before != current) {
        resultList.push_back(current);
        before = drum.at(current);
        current = before;
    }

    resultList.push_back(intrare);
    std::cout << "drum: ";

    for (int i = 0; i < resultList.size(); i++)
        std::cout << resultList[i];
}

};

int main(){

Graf graf;

graf.citire();
graf.tranzitie();
graf.PBF();
system("pause");
return 0;

}

0 个答案:

没有答案