-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomWalk.cpp
More file actions
70 lines (52 loc) · 1.14 KB
/
Copy pathRandomWalk.cpp
File metadata and controls
70 lines (52 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main()
{
int n, m;
cout << "Enter rows and columns: ";
cin >> n >> m;
int grid[50][50] = {0};
int visited = 1;
int total = n * m;
int r = n / 2;
int c = m / 2;
grid[r][c] = 1;
int moves = 0;
int dr[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
int dc[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
srand(time(0));
while (visited < total)
{
int dir = rand() % 8;
int nr = r + dr[dir];
int nc = c + dc[dir];
if (nr >= 0 && nr < n && nc >= 0 && nc < m)
{
r = nr;
c = nc;
if (grid[r][c] == 0)
{
visited++;
grid[r][c] = visited;
}
moves++;
cout << "\nMove " << moves << ":\n";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
if (grid[i][j] == 0)
cout << ".\t";
else
cout << grid[i][j] << "\t";
}
cout << endl;
}
}
}
cout << "\nAll tiles visited!\n";
cout << "Total moves: " << moves << endl;
return 0;
}