简体   繁体   中英

Data Structure in C++

I am working on a problem and implementing an algorithm in C++. The algorithm requires a data structure in which it is similar to a 2D array (say a 20x20 array). The main different is that every cell must connect to the eight neigbours around it (ie up, down, left, right and four corners).

The status of each member will change based on the data changes of the neighbors. So, every cell grow dynamically. Each cell needs to check the data of all its neighbours constantly.

Based on that requirement, I imagine this data structure is circular, like a torus or a bagel, which has no edge, so that every cell is interconnected to each other.

Any idea on the representation of this data structure? I am thinking to use a graph of adjacency linked list, in which each member contains a linked list of the surrounding eight neighbors. What do you think? Am I on the right track?

Look for an implementation of the Game of Life, which does this with zeros and ones. Unless you are doing something very sophisticated by converging a solution to a set of constraints for each iteration, you will just loop through your array each time, referencing the last complete generation, and update everything to create the new generation at the end of each loop.

Mostly it depends on your problem, but I have my doubts regarding adjacency linked list. That would be more suitable if your neighbours were growing dynamically, but in this case they seem to be fixed. So you might as well just use an array that points to your neighbours.

Problem statement seems not very clear:

The status of each member will change based on the data changes of the neighbors. So, every cell grow dynamically. Each cell needs to check the data of all its neighbours constantly.

what does this actually mean? Lets assume just one value changes. Then all the neighbours should change, and all their neighbours change again, until ALL values have changed. But what about the original value, after a change it's neighbours change, should it change again in response (and keep changing indefinitely - sounds like a bad idea)?

What about this: we have a simple case of a 1x4 2D array ABCD where A is a neighbour of B and D, and B of A and C, etc.

Say A changes. So should B and D. Now, C should change - should it instantly change based on both changes in B and D? or B first, D second? or what?

What is the meaning/definition of constantly and dynamically in your problem? Are there time steps, eg

 time 1: a cell changes
 time 2: all immediate neighbours change simultaneously
 time 3: neighbours of immediate neighbours change
         (and what about the original cell at this point?)
 time 4: etc.

In most cases I would (as most others) suggest a 2D-array based structure, but with a setter method, which, upon invocation, would do change propagation atomically. But it really depends on your the definition of constantly and dynamically

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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