繁体   English   中英

如何从任何迷宫 ASCII 文本文件中创建动态分配的二维数组?

[英]How can I create a dynamically-allocated 2D array out of any maze ASCII text file?

我找到了一种将任何 ASCII 文件转换为字符串的简单方法,无论其尺寸如何,但这对我没有多大帮助,因为我需要在动态分配的二维数组中使用它? 稍后我想将哪些属性转换为 Graph 属性以解决迷宫问题。 从我的字符串中获取动态分配的二维数组的最佳方法是什么,或者——如果我的方法不是最好的——从带有迷宫的 ASCII 文本文件中获取? 我希望能够将 2d 数组中的空白转换为顶点,并将它们与我将有一个起始和结束顶点的边缘连接起来。

std::ifstream in("d:\\mazes\\mymaze.txt");
std::string s((std::istreambuf_iterator<char>(in)),
std::istreambuf_iterator<char>());
cout << s;

这应该工作:

int rows = 4;//When you change these the array will change size
int cols = 4;

// declaration
int ** a;

// allocate
a = new int*[rows];
for(int i = 0; i < rows; i++)
    a[i] = new int[cols];

// set the values
for(int j = 0; j < rows; j++)
    for(int i = 0; i < rows; i++)
        a[i][j] = 0;

// destruct
for(int i = 0; i < rows; i++)
    delete a[i];
delete a[];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM