Jump to content

Simulating threads

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
havocado

havocado

    Newbie

  • Members
  • Pip
  • 1 posts
Suppose I have a matrix HEIGHT X WIDTH, and each cell is either empty or has some sort of point (some enum or integer which will indicate its direction) in it. Now, using a while loop each point will move one cell at a time in a specific direction. What I need to do is, when two points enter the same cell, they will both disappear. So it's kind of like each point is a thread since they all have to move at once (= one iteration of the loop), but I can't use threads.

I am not sure how to approach this. And I will be very grateful if someone will give me a direction for a nice way to solve this problem.

Thanks in advance.

#2
Vswe

Vswe

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 9,552 posts
You can just move them one by one, but instead of moving them inside your matrix you move them to a new matrix of the same size. Then the new matrix will be empty in the beginning, if you then try to move something to an already existing cell, empty the cell instead. I'm pretty sure that didn't made much sense but I can give some examples.

0 - Empty
1 - Up
2 - Left
3 - Down
4 - Right

The matrix:
4 4 0

0 3 1

4 0 2

The new matrix:
0 0 0

0 0 0

0 0 0

Then loop through all the cells:
(the top-left 4)
0 [B]4[/B] 0

0 0 0

0 0 0

(the top-middle 4)
0 4 [B]4[/B]

0 0 0

0 0 0

(the mid-middle 3)
0 4 4

0 0 0

0 [B]3[/B] 0

(the mid-right 1)
Now there will be a crash, mark it as a crash.
0 4 [B]-1[/B]

0 0 0

0 3 0

(the boottom-left 4)
And now another crash.
0 4 -1

0 0 0

0 [B]-1[/B] 0

And now when the 2 in the bottom-right corner is moving there's a -1 there, also a crash. Now you can just replace the -1 with 0.

0 4 0

0 0 0

0 0 0

This is probably only one solution :)