Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise07.cpp
blob: e6f22710582ff8ef9dd8215abac3645fd080b6e4 (plain) (blame)
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
#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;
}