The Biham–Middleton–Levine traffic model is a simple cellular automata model loosely mimicking traffic flow. In an m-by-n domain, we see white empty space (or 0 in our matrix representation), red cars (1 in the matrix), and blue cars (2 in the matrix).
Assume the space is toroidal. That is to say, the right side connects with the left, and the top connects to the bottom. So a red car that moves off the far right of the matrix re-appears on the far left.
Here is a 4-by-4 version with three red cars and two blue cars.
0 0 0 2
1 1 0 0
0 0 2 0
0 0 0 1
Red cars always move to the right if they are unblocked. A red car can move either into an empty space or a space being vacated by a moving red car.
After we move the red cars (1s) we will have this matrix.
0 0 0 2
0 1 1 0
0 0 2 0
1 0 0 0
We're only halfway through the process. After we move the blue cars (2s) we end up here.
0 0 0 0
0 1 1 2
0 0 0 0
1 0 2 0
This last value of the matrix would be the return value of your function. Assume that red cars always move before blue cars.
Solution Stats
Problem Comments
1 Comment
Solution Comments
Show comments
Loading...
Problem Recent Solvers38
Suggested Problems
-
3095 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1284 Solvers
-
1296 Solvers
-
172 Solvers
-
212 Solvers
More from this Author54
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
That was hard, but a good problem.
Also, the referenced simulation site is still operational after over a decade and worth a look.