Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise07.cpp
diff options
context:
space:
mode:
Commit5ce214686e668bf185222b518b78d7f47a20b4fd (patch)
Author
 Syndamia <kamen@syndamia.com>
2025-12-07 13:44:16 +0200
Committer
 Syndamia <kamen@syndamia.com>
2025-12-07 13:44:16 +0200
Filesacd2c1e0f3f0c7aa850878a12a9b674b8be1ccf7 /Week10/Exercise07.cpp
upp-2025-solutions-5ce214686e668bf185222b518b78d7f47a20b4fd.tar
upp-2025-solutions-5ce214686e668bf185222b518b78d7f47a20b4fd.tar.gz
upp-2025-solutions-5ce214686e668bf185222b518b78d7f47a20b4fd.zip
Parent6f894be622e8046f43a70e843cff7bd99c58a3c1 (diff)
feat(Week10): Add solutions
1 files changed, 64 insertions, 0 deletions
-rw-r--r--Week10/Exercise07.cpp64
diff --git a/Week10/Exercise07.cpp b/Week10/Exercise07.cpp
new file mode 100644
index 0000000..e6f2271
--- /dev/null
+++ b/Week10/Exercise07.cpp
@@ -0,0 +1,64 @@
+#include <iostream>
+
+bool** allocateMatrix(unsigned int rows, unsigned int cols) {
+ bool** mat = new bool*[rows];
+ for (unsigned int row = 0; row < rows; row++) {
+ mat[row] = new bool[cols];
+ for (unsigned int col = 0; col < cols; col++)
+ mat[row][col] = false;
+ }
+
+ return mat;
+}
+
+void deleteMatrix(bool** matrix, unsigned int rows) {
+ for (unsigned int row = 0; row < rows; row++)
+ delete[] matrix[row];
+ delete[] matrix;
+}
+
+int main() {
+ unsigned int allocatedRows = 4, allocatedCols = 4;
+ bool** mat = allocateMatrix(allocatedRows, allocatedCols);
+
+ int row = 0, col = 0;
+ while (row >= 0 && col >= 0 && !mat[row][col]) {
+ mat[row][col] = true;
+
+ char dir;
+ std::cin >> dir;
+ switch (dir) {
+ case 'r': col++; break;
+ case 'l': col--; break;
+ case 'u': row--; break;
+ case 'd': row++; break;
+ }
+
+ if (row >= allocatedRows || col >= allocatedCols) {
+ bool** largerMat = allocateMatrix(allocatedRows * 2, allocatedCols * 2);
+
+ for (unsigned int row = 0; row < allocatedRows; row++)
+ for (unsigned int col = 0; col < allocatedCols; col++)
+ largerMat[row][col] = mat[row][col];
+
+ deleteMatrix(mat, allocatedRows);
+ mat = largerMat;
+
+ allocatedRows *= 2;
+ allocatedCols *= 2;
+ }
+ }
+
+ unsigned int i, j;
+ std::cin >> i >> j;
+ if (i < allocatedRows && j < allocatedCols && mat[i][j]) {
+ std::cout << "Visited" << std::endl;
+ }
+ else {
+ std::cout << "Not visited" << std::endl;
+ }
+
+ deleteMatrix(mat, allocatedRows);
+
+ return 0;
+}