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.
Simulating threads
Started by havocado, Jan 07 2011 08:39 AM
1 reply to this topic
#1
Posted 07 January 2011 - 08:39 AM
|
|
|
#2
Posted 07 January 2011 - 03:09 PM
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:
The new matrix:
Then loop through all the cells:
(the top-left 4)
(the top-middle 4)
(the mid-middle 3)
(the mid-right 1)
Now there will be a crash, mark it as a crash.
(the boottom-left 4)
And now another crash.
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.
This is probably only one solution :)
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 :)


Sign In
Create Account

Back to top









