开发者

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

I found a simple way to convert any ASCII file into a string regardless of its dimensions, but this doesn't help me a lot because I need it in a dynamically-allocated 2D array? Which attributes I want to convert later on to Graph attributes to solve the maze. What is the best way to get a dynamically-allocated 2D array out of my string or—if my approach is not the best one—out of the ASCII text file with the maze? I want to be able to c开发者_StackOverflowonvert white space in the 2d Array to vertices and connect them with edges where I will have a starting and ending vertex.

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


This Should Work:

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[];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜