I
I
icegreenberry2010-10-19 00:29:07
Algorithms
icegreenberry, 2010-10-19 00:29:07

How to display a labyrinth from a text file as a two-dimensional array?

Hello. It’s late now and maybe I’m slowing down a little, but somehow it doesn’t occur to me how to do the following:
let’s say in a text file in a two-dimensional Cartesian coordinate system the simplest labyrinth is presented in the form of units (walls), zeros (rooms) and twos (finish). So Thus, the two is located at the coordinates (4,5)
1,1,1,2,1,1
1,0,0,0,0,1
1,0,1,1,0,1
1,0,0,1 ,0,1
1,1,1,1,1,1
How do I convert this set of numbers into a two-dimensional array so that each value in the array has a key, like in a Cartesian coordinate system? That is, so that the deuce is in array[4][5]

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
belk, 2010-10-19
@belk

for(j=5;j>=1;j--)
  for(i=1;i<=6;i++)
    scanf("%i",&array[i][j]);

Something like this.

M
mardy_bum, 2010-10-19
@mardy_bum

I don't understand why you can't just read the file as usual?

[
[email protected]><e, 2010-10-19
@barmaley_exe

If we know the dimensions of the maze in advance (or we can read them), then we just need to write at the address matrix[n - i - 1][j] (you can add one to each index so that the numbering goes from 1, but then the memory is also needed highlight more - (n + 1) * (m + 1)).
If the dimensions of the matrix are not known in advance, and we must determine its dimensions in the process of reading, then it is better, I think, to read the matrix as usual, and then wrap it in a class with get and set methods (or, if the language allows, install getters and setters), in which already produce the necessary offsets, based on the size of the matrix, which will be stored in the same class.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question