-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdynamic.cpp
157 lines (134 loc) · 4.41 KB
/
dynamic.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <ctime>
#include <cstdlib>
#include <cmath>
using namespace std;
// Function to generate a random matrix with 10% '1's and 30% of them are '2's
vector<vector<int>> generateRandomMatrix(int rows, int cols, int &zeroCount, int &oneCount, int &twoCount)
{
vector<vector<int>> matrix(rows, vector<int>(cols, 0));
int totalCells = rows * cols;
int onePercentage = 20;
int twoPercentage = 30;
int zeroPercentage = 100 - onePercentage - twoPercentage;
oneCount = ((totalCells * onePercentage) / 100.0);
twoCount = ((oneCount * twoPercentage) / 100.0);
zeroCount = totalCells - oneCount - twoCount;
// Set '1's (fixed elements)
while (oneCount > 0)
{
int x = rand() % rows;
int y = rand() % cols;
if (matrix[x][y] == 0)
{
matrix[x][y] = 1;
oneCount--;
}
}
// Set '2's (moving elements)
while (twoCount > 0)
{
int x = rand() % rows;
int y = rand() % cols;
if (matrix[x][y] == 0)
{
matrix[x][y] = 2;
twoCount--;
}
}
return matrix;
}
// Function to print the matrix
void printMatrix(const vector<vector<int>> &matrix, int rows, int cols)
{
cout << " ";
for (int i = 0; i < cols; i++)
{
cout << i << " ";
}
cout << endl;
cout << endl;
cout << endl;
cout << endl;
int j = 0;
for (const auto &row : matrix)
{
cout << j << " ";
for (int val : row)
{
cout << val << " ";
}
cout << endl;
cout << endl;
cout << endl;
j++;
}
}
int main()
{
srand(static_cast<unsigned>(time(0)));
int rows, cols;
cout << "Enter the number of rows: ";
cin >> rows;
cout << "Enter the number of columns: ";
cin >> cols;
int zeroCount, oneCount, twoCount;
vector<vector<int>> matrix = generateRandomMatrix(rows, cols, zeroCount, oneCount, twoCount);
cout << "Initial Random Matrix:" << endl;
printMatrix(matrix, rows, cols);
int startX, startY;
cout << "Enter the starting X-coordinate of the moving element: ";
cin >> startX;
cout << "Enter the starting Y-coordinate of the moving element: ";
cin >> startY;
if (startX < 0 || startX >= rows || startY < 0 || startY >= cols || matrix[startX][startY] != 2)
{
cout << "Invalid starting coordinates for the moving element." << endl;
return 1;
}
int numMovements;
cout << "Enter the number of movements: ";
cin >> numMovements;
cout << "\nCounts in the Initial Matrix:" << endl;
cout << "Number of '0's: " << zeroCount << endl;
cout << "Number of '1's: " << oneCount << endl;
cout << "Number of '2's: " << twoCount << endl;
// Perform movements
int currentX = startX, currentY = startY;
for (int movement = 1; movement <= numMovements; ++movement)
{
int dx = rand() % 3 - 1; // Random movement in x direction (-1, 0, 1)
int dy = rand() % 3 - 1; // Random movement in y direction (-1, 0, 1)
// Calculate the new position
int newX = currentX + dx;
int newY = currentY + dy;
// Check if the new position is within bounds
if (newX >= 0 && newX < rows && newY >= 0 && newY < cols)
{
// Check if the new position is a valid move (0) or the same position
if (matrix[newX][newY] == 0 || (newX == currentX && newY == currentY))
{
if (newX == currentX && newY == currentY)
{
cout << "Same Position" << endl;
}
matrix[currentX][currentY] = 0;
matrix[newX][newY] = 2;
currentX = newX;
currentY = newY;
}
}
else
{
// If the new position is out of bounds, print the same position
cout << "\nMovement " << movement << ": Staying at (" << currentX << ", " << currentY << ")" << endl;
cout << endl;
}
// Print the updated matrix after the movement
cout << "\nMovement " << movement << ":" << endl;
cout << endl;
printMatrix(matrix, rows, cols);
}
return 0;
}