Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to model a grid data type?

How can I model a grid(minesweeper problem) as a data type in the best possible way? Is it better to use a vector as 2 dimensional entity. The reason for a vector is because of its bounds checking capabilities. Is my assumption right?

Thanks :)

like image 795
jaykumarark Avatar asked Sep 14 '26 20:09

jaykumarark


2 Answers

Since the grid-size is (I presume!) fixed, a vector gives you little advantage over an array. So I recommend going with a 2D array.

While not necessary, you might find it useful to add sentinel values around the boundary of the grid to remove the need for implicit bounds checking. An example is placing 0's all around the grid for when you're summing the number of neighbouring mines. Read this article for some simpler examples to begin with.

like image 193
moinudin Avatar answered Sep 17 '26 09:09

moinudin


I personally think that the vest option is to use a specialized class that acts like a multidimensional array while giving you the benefits of the STL vector (size-aware, no implicit conversions to pointers, etc.). Boost's multi_array class might be a good candidate here, and more generally whenever you need a multidimensional array. It handles all of the weirdness normally associated with raw C++ arrays without sacrificing performance.

like image 34
templatetypedef Avatar answered Sep 17 '26 10:09

templatetypedef



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!