I used to read grids with the second method below but this one is much simpler! I had to save it here for future references, borrowed from marioyc. In this specific example (469Wetlands of Florida), we are not given the dimensions of grid (ugh).
1
LLLLLLLLL
LLWWLLWLL
LWWLLLLLL
Or if you know the dimensions,
2D grid with no spaces between cells
This is done using std::getline like above, but here we save the grid as a 2D char array instead of multiple vectors of strings (above). In this particular, we know the dimensions but not the total number of tests.
3
111
222
333
2
444
333
Unbounded tests and variable length strings (use sstream)
We have a bunch of tests but we don’t know the number of tests. This is an example:
5
E 0.01 *A
...
10
S 2.23 Q*
A 9.76 CKM
...
Each test case starts with the number of lines in the test case. Each line contains three values and so we use std::istringstream to extract the values. The following works although not pretty.
2D grid with ints and characters
This is a square grid with 5 rows and 5 columns. Also, we’re only given the lower triangle only in the matrix since this is an unweighted graph. The main issue was that an ‘x’ was used to indicate that there is no edge between the vertices.
5
50
10 x
x 20 40
40 x 30 60
3D grid with no space between cells
This is from problem “532 - Dungeon Master”. Here we know when to terminate. We also know the number of rows, colums and the height (number of levels). There is also an extra new line between each board.
2 4 5
SXXXX
X@@@X
X@@XX
@@@X@
@@@@@
@@@@@
@@X@@
@@XXX
1 3 3
S@@
@E@
@@@
0 0 0
Strings with variable number of ints
Consider a number of lines where each line consists of a variable number of ints. We can use std::istringstream and use a while loop to read as many ints as possible from that line.