The Problem: There are a number of problems known collectively as "Random Walk" problems that have been of longstanding interest to the science community. All but the most simple of these are extremely difficult to solve and for the most part they remain largely unsolved. One such problem may be stated as follows: A (drunken) bug is placed on a given square in the middle of a tile floor in a rectangular room of size (N x M) tiles. The bug wanders (possibly in search of an aspirin) randomly from tile to tile throughout the room. Assuming that it may move from its present tile to any of the eight surrounding tiles (unless it is against a wall) with equal probability, how many moves will it take to touch every tile on the floor at least once? Hard as this problem may be to solve by probability theory techniques, it is easy to solve using the computer. The technique for doing so is called "Simulation”. This technique is widely used in industry to predict traffic flow, inventory control, and so forth. The problem may be simulated using the following method: Methodology: An (N x M) array is used to represent the number of times Cij our bug has reached a tile at position (i , j) on the floor. All the cells of this array are initialized to Zero before simulation starts. At the start, the position of the bug is set to (istart, jstart). The eight possible moves of the bug are represented by the tiles located at (i + imove[k] , j + jmove[k]), where 1<= k <= 8 and imove[l ] = -1 jmove[l ] = 1 imove[2] = 0 jmove[2] = 1 7 8 1 6 i,j 2 5 4 3 imove[3] = 1 jmove[3] = 1 imove[4] = 1 jmove[4] = 0 imove[5] = 1 jmove[5] = -1 imove[6] = 0 jmove[6] = -1 imove[7] = -1 jmove[7] = -1 imove[8] = -1 jmove[8] = 0 Generating a random value for k lying between 1 and 8 simulates a random walk to one of the eight given squares. Of course, the bug cannot move outside the room, so coordinates that leap up a wall must be ignored and a new random combination formed. Each time a square is entered the count Cij for that square is incremented so that a nonzero entry shows the number of times the bug has landed on that square so far. When every square has been entered at least once, the experiment is complete. Program Specification: Write a program to perform the specified simulation experiment. The program should provide the following: • Arbitrary values of N and M , 2 < N <= 20 , 2 < M <= 40 with dynamic (Run-time) allocation. • Arbitrary starting positions (istart, jstart). • An iteration limit, that is, a maximum number of squares the bug may enter during the experiment (this avoids getting in an infinite loop); a maximum of 50,000 is appropriate for this exercise. • For each experiment, a display of: 1. The total number of legal moves that the bug made 2. The final Cij array (this will show the density of the walk, i.e., the number of times each tile on the floor was touched during the experiment). You should show the array of counts in character symbols on the screen using the following scheme: Let the average count in a tile be , then use: Symbol 1 for 1 ≤ Cij < 0.5 x 2 for 0.5 x ≤ Cij < x 3 for x ≤ Cij< 1.5 x 4 for 1.5 x ≤ Cij < 2x 5 for Cij ≥ 2x 0 for zero counts Some Design Guidelines: • You will need to create the space for Cij dynamically. That space will be a run-time 2-D array of size N*M tiles. • Build a function to initialize the counts Cij to zeros. • Build a function to display the array on the screen. It computes the average count (x) and uses the scheme given above. • Build a function to generate one random move out of 8 possible moves. It receives the old position and returns the new position of the bug. • Build a Boolean function to return true only if the move is inside the room |
|
|
void main()
with int main()
|
|
system("pause");
.
|
|