Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

two dimensional array, matrix

Status
Not open for further replies.

manichandra

Technical User
Feb 6, 2012
19
US
can any body give me the idea to solve this problem, not the whole answer.thanks,

Use a matrix represents an island surrounded by water. Two bridges lead out of
the island. A mouse is placed on the black square. Write a program to make the
mouse take a walk across the island. The mouse is allowed to travel one square
at a time, either horizontally or vertically. A random number from 1 through 4
should be used to decide which direction the mouse is to take. The mouse drowns
when he hits the water; he escapes when he enters a bridge. You may generate a
random number up to 100 times. If the mouse does not find his way by the
hundredth try, he will die of starvation. Restart the mouse in a reinitialized
array and go back and repeat the whole process. Count the number of times he
escapes, drowns, and starves.
Input File
1. First input line - the size of the array, including border of water and
bridges (not larger than 20 X 20)
2. Next N input lines-the rows of the two-dimensional array, where the
positions containing negative numbers represent the water, the positions in
the edge containing a 0 represent the bridges, the position containing a 1
represents the starting position of the mouse, and all other positions
contain 0s.
Output
1. A line stating whether the mouse escaped, drowned, or starved
2. A line showing the mouse's starting position and the position of the two
bridges
3. A map showing the frequency of the mouse's visits to each position
 
Break it into little steps and solve each step in turn.

1) Start by declaring a 2D array and reading in the array as specified in Input 1 and input 2. Call this map. Seed your random number generator (use srand)

2) Look for the position containing a 1 and print the row and column and save it.

3) Declare a second 2D array and set all values to zero. Call this frequency.

4) Generate a random number (use rand)

5) If 1 move 1 row up, 2, move one column right, 3, move one row down, 4 move one column left.

6) Increment position in frequency. Check the location in the array where the mouse will end up on map.

7) Repeat from step 4 for 100 times until either the map position is 0 or -1.

8) if last map position is 0, mouse escaped, 1, mouse starved, -1, mouse drowned (output 1)

9) Print position saved in step 2 (output 2)

10) Print the frequency array (output 3)

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top