Random Walk (walk)

Source: Internet
Author: User

1. Topics

One type of problem is always called random Walk, which has long attracted the interest of the mathematics community. All these problems, even the simplest, are extremely difficult to solve. And they are far from being resolved to a large extent. One such problem can be described as:

In a rectangular room with nxm tiles, a (drunken) cockroach is placed in a designated square in the middle of the floor. Cockroaches randomly "wander" from a tile to another tile (probably looking for an aspirin). Suppose it could move from its tile to any of the eight tiles around it (unless it touches a wall), how long will it take to touch each tile at least once?

Although this problem may be difficult to solve with purely probabilistic techniques, it is very easy to use a computer. The technique of using a computer to solve this problem is known as "impersonation." This technology is widely used in industry to predict transportation flow, inventory control and so on. The problem can be simulated in the following ways:

A NXM array is used as a counter to indicate how many times the cockroach reaches each tile, and the initialization of each array unit is set to zero. The position of the cockroach on the floor is indicated by the coordinates (CURRENTR,CURRENTC). The eight types of cockroaches that may be moved with a tile representation in position (NEXTR = currentr + IMOVE[K],NEXTC = CurrentC + jmove[k]), where 0≤k≤7, and

Imove[0] =-1;   Jmove[0] = 1; Northeast side
IMOVE[1] = 0;   JMOVE[1] = 1; Zheng Dong
IMOVE[2] = 1;   JMOVE[2] = 1; South East
IMOVE[3] = 1;   JMOVE[3] = 0; Just south.
IMOVE[4] = 1;  JMOVE[4] =-1; Southwest Side
IMOVE[5] = 0;  JMOVE[5] =-1; Just West
IMOVE[6] =-1;  JMOVE[6] =-1; Northwest Side
IMOVE[7] =-1;   JMOVE[7] = 1; Just north.

A random stroll of eight squares adjacent to the cockroach is simulated by generating a random value K (0≤k≤7). Of course, cockroaches cannot climb out of the room, so they should remove the coordinates to the wall and form a new random combination. Every time a cockroach enters a square, the counter of the square increases by 1, and a non-0 element of the counter indicates the number of times the cockroach has reached the corresponding square. Every time a square is entered at least once, the experiment is complete.

2. Conditions

The program must meet:

① is capable of handling all n and M values, N and M satisfying:2<n≤40,2≤m≤20;

② is able to experiment with "n = 15,m = 15, starting point (10,10)" and "n = 39,m = 19, starting point is ()".

The ③ has an iterative limitation that the program can terminate when the maximum number of times a cockroach enters a block is Max =50000 during the experiment.

3. Output

For each test, print:

① the total number of legal moves the cockroach makes.

② The final counter array, showing the "density" of the walk, that is, the number of times each tile in the experiment was received.

4. SOURCE program
#include <iostream> #include <cstdlib> #include <ctime>using namespace std;int const MAX_LENGTH = 100;    int random (int m,int n)//Specify the range of random number {int Pos,dis;    if (M = = N)//indicates that there is only one digit return m in the range;        else if (M > N)//indicates the number in the interval [m,n] {pos = n;        dis = m-n + 1;    Return rand ()% dis + pos;        } else//indicates the number in the interval [n,m] {pos = m;        dis = n-m + 1;    Return rand ()% dis + pos;  }}int Rmove[8],cmove[8];    Moving azimuth array void initmove (int* imove,int* jmove)//Initialize moving azimuth array {imove[0] =-1;   Jmove[0] = 1;    Northeast side Imove[1] = 0;   JMOVE[1] = 1;    Just east imove[2] = 1;   JMOVE[2] = 1;    South East Imove[3] = 1;   JMOVE[3] = 0;    Just south imove[4] = 1;  JMOVE[4] =-1;    Southwest Side Imove[5] = 0;  JMOVE[5] =-1;    Positive Western Imove[6] =-1;  JMOVE[6] =-1;    Northwest Square Imove[7] =-1;   JMOVE[7] = 1;  Just North}bool judgewalk (int* mark[],int n,int m)//Determine if all tiles are accessed {for (int i = 0; i < n; i++) {for (int j = 0; j < m;J + +) {if (!mark[i][j])//If there is a tile that is not finished, exit return false; }} return true; void Printwalk (int* mark[],int n,int m)//print all tile access times {for (int i = 0; i < n; i++) {for (int j = 0; J < m ;        J + +) {cout << mark[i][j] << "";    } cout << Endl;  }}void Randomwalk (int** mark,int n,int m)//random Walk {srand ((int) time (NULL));//The corresponding seed value is generated initmove (rmove,cmove) Initializes an array of moving azimuth int currentr = Random (0,n-1); Now where the row int currentc = Random (0,m-1);    Now the column mark[currentr][currentc]++;    int NEXTR,NEXTC;        Next position the row, the next position where the column int num = 0;   Number of moves while (true) {int moving = Random (0,7);   Randomly generated movement direction NEXTR = Currentr + rmove[moving];   Next position Row NEXTC = CurrentC + cmove[moving]; Next position column while (nextr < 0 | | NEXTC < 0 | | NEXTR >=n | |   NEXTC >= m)//when touching the wall {moving = Random (0,7); Randomly generated movement direction nextr = CURrentr + rmove[moving];   Next position Row NEXTC = CurrentC + cmove[moving];   Next position in column} mark[nextr][nextc]++;        Access +1 currentr = nextr;   CurrentC = NEXTC;        The next position equals the current position num++;  if (Judgewalk (mark,n,m))//Determine if you have finished accessing all tiles {cout << total move step: << num << Endl;    Print total number of steps Printwalk (MARK,N,M);        Print all tile access times return; } if (num > 50000000)//exceeds the access limit number {cout << "cockroach don t visit all Tile." << End            L  cout << "Total move step:" << num << Endl;            Print total number of steps Printwalk (MARK,N,M);        Return  }}}int Main () {int *mark[max_length];    pointer array for (int i = 0; i < max_length; i++)//dynamic assignment mark[i] = new Int[max_length];    for (int i = 0; i < max_length; i++)//Initialize for (int j = 0; J < max_length;j++) Mark[i][j] = 0;  int n,m; Rows of Cin >> n >> m;   Randomwalk (MARK,N,M);    for (int i = 0; i < max_length; i++)//Free space delete[] mark[i]; return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Random Walk (walk)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.