Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/hadoop/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;读取文本文件,以类似方式显示_C++_Text Files_Path Finding - Fatal编程技术网

C++ c++;读取文本文件,以类似方式显示

C++ c++;读取文本文件,以类似方式显示,c++,text-files,path-finding,C++,Text Files,Path Finding,首先,请原谅我,我的编码知识充其量是贫乏的。 我已经阅读了很多指南,我一生都无法解决这个问题 我在玩以前在这个网站上展示的代码,如图所示: #include <iostream> #include <string> #include <cmath> #include <vector> #include <utility> #include <algorithm> #include <queue> using

首先,请原谅我,我的编码知识充其量是贫乏的。 我已经阅读了很多指南,我一生都无法解决这个问题

我在玩以前在这个网站上展示的代码,如图所示:

#include <iostream>
#include <string>
#include <cmath>
#include <vector>
#include <utility>
#include <algorithm>
#include <queue> 

using namespace std;

class CNode
{
public:

CNode() : xPos(0), yPos(0), travelCost(0) {}
CNode(int x, int y) : xPos(x), yPos(y), travelCost(0) {}
CNode(int x, int y, int cost) : xPos(x), yPos(y), travelCost(cost) {}

inline CNode& operator=(const CNode& target)
{
    if (*this != target)
    {
        xPos = target.xPos;
        yPos = target.yPos;
        travelCost = target.travelCost;
    }

    return *this;
}

inline bool operator==(const CNode& target) const
{
    return xPos == target.xPos && yPos == target.yPos;
}

inline bool operator!=(const CNode& target) const
{
    return !(*this == target);
}

inline bool operator<(const CNode& target) const
{
    return target.travelCost < travelCost;
}

int xPos, yPos, travelCost;
};

class CPath
{
public:

typedef vector<CNode> nodeList;

nodeList Find(const CNode& startNode, const CNode& endNode, int mapArray[][20])
{
    nodeList finalPath, openList, closedList;

    finalPath.push_back(startNode);
    openList.push_back(startNode);
    closedList.push_back(startNode);

    while (!openList.empty())
    {
        // Check each node in the open list
        for (size_t i = 0; i < openList.size(); ++i)
        {
            if (openList[i].xPos == endNode.xPos && openList[i].yPos == endNode.yPos)
                return finalPath;

            priority_queue<CNode> nodeQueue;

            // Get surrounding nodes
            for (int x = -1; x <= 1; ++x)
            {
                for (int y = -1; y <= 1; ++y)
                {
                    const int current_x = openList[i].xPos + x;
                    const int current_y = openList[i].yPos + y;

                    bool alreadyCheckedNode = false;
                    for (size_t i = 0; i < closedList.size(); ++i)
                    {
                        if (current_x == closedList[i].xPos && current_y == closedList[i].yPos)
                        {
                            alreadyCheckedNode = true;
                            break;
                        }
                    }

                    if (alreadyCheckedNode)
                        continue;

                    // Ignore current coordinate and don't go out of array scope
                    if (current_x < 0 || current_x > 20 || current_y < 0 ||current_y > 20 || (openList[i].xPos == current_x && openList[i].yPos == current_y))
                        continue;

                    // Ignore walls
                    if (mapArray[current_x][current_y] == '#')
                        continue;

                    const int xNodeDifference = abs(current_x - (openList[i].xPos));
                    const int yNodeDifference = abs(current_y - (openList[i].yPos));            

                    // Diagonal?
                    const int direction = xNodeDifference == 1 && yNodeDifference == 1 ? 14 : 10;

                    const int xDistance = abs(current_x - endNode.xPos);
                    const int yDistance = abs(current_y - endNode.yPos);
                    int heuristic = 10 * (xDistance + yDistance);

                    nodeQueue.push(CNode(current_x, current_y, heuristic));
                }
            }

            if (!nodeQueue.empty())
            {
                // Add the nearest node
                openList.push_back(nodeQueue.top());
                finalPath.push_back(nodeQueue.top());

                // Put into closed list
                while (!nodeQueue.empty())
                {
                    closedList.push_back(nodeQueue.top());
                    nodeQueue.pop();
                }
            }
        }
    }

    return finalPath;
}
};

int mapArray[20][20] =
{
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },
{ '#', 'A', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '#' },
{ '#', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', 'B', '#' },
{ '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#', '#' },

};

int main(int argc, char** argv)
{
CNode start, end;

for (int width = 0; width < 20; ++width)
{
    for (int height = 0; height < 20; ++height)
    {
        if (mapArray[width][height] == 'A')
        {
            start.xPos = width;
            start.yPos = height;
        }
        else if (mapArray[width][height] == 'B')
        {
            end.xPos = width;
            end.yPos = height;
        }
    }
}

CPath pathFinder;
CPath::nodeList n = pathFinder.Find(start, end, mapArray);

for (int i = 0; i < n.size(); ++i)
    if (mapArray[n[i].xPos][n[i].yPos] != 'A' && mapArray[n[i].xPos][n[i].yPos] != 'B')
        mapArray[n[i].xPos][n[i].yPos] = '*';

for (int height = 0; height < 20; ++height)
{
    for (int width = 0; width < 20; ++width)
    {
        if (width % 20 == 0)
            cout << endl;

        cout << (char)mapArray[height][width] << " ";
    }
}

cin.get();

return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类CNode
{
公众:
CNode():xPos(0)、yPos(0)、travelCost(0){}
CNode(intx,inty):xPos(x),yPos(y),travelCost(0){}
CNode(整数x,整数y,整数成本):XPO(x),YPO(y),旅行成本(成本){}
内联CNode和运算符=(常数CNode和目标)
{
如果(*此!=目标)
{
xPos=target.xPos;
yPos=target.yPos;
travelCost=target.travelCost;
}
归还*这个;
}
内联布尔运算符==(常量CNode和目标)常量
{
返回xPos==target.xPos&&yPos==target.yPos;
}
内联布尔运算符!=(常数CNode和目标)常数
{
返回!(*此==目标);
}

内联布尔运算符哪个部分给您带来了麻烦?读取文件、填充数据结构或显示结果?可耻的是——老实说,以上所有内容。请在文本文件中发布数据格式的片段。它只是一个包含1和0的文件,以类似矩阵的形状排列,即1101000 000100000000000使用2D数组或字符。使用GetLine读取文件的每一行,如果是1,则为每个字符在数组中的正确位置添加一个“#”,否则设置为“”,即是如此。然后只需使用std::cout从上到下打印数组中的每个字符(将std::endl添加到每一行的末尾)。