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
|
/*
Task 2 from
https://perlweeklychallenge.org/blog/perl-weekly-challenge-077/
Comments: https://andrewshitov.com/2020/09/08/lonely-x-the-weekly-challenge-77-task-2/
Compile as:
$ g++ -std=c++17 ch-2.cpp
Output for the given example of matrix:
$ ./a.out
0, 3
1, 1
2, 3
*/
#include <iostream>
#include <vector>
using namespace std;
int test_move_calls = 0;
vector<int> test_move(vector<vector<char>> matrix, vector<int> current, vector<int> shift) {
test_move_calls++;
current[0] += shift[0];
current[1] += shift[1];
if (current[0] < 0 || current[0] >= matrix.size() ||
current[1] < 0 || current[1] >= matrix[0].size()) {
return vector<int>();
}
else {
return current;
}
}
int main() {
vector<vector<char>> matrix = {
{'X', 'O', 'O', 'X'},
{'O', 'O', 'X', 'O'},
{'X', 'O', 'O', 'X'}
};
vector<vector<int>> neighbours = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}, {1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
for (auto row = 0; row != matrix.size(); row++) {
for (auto col = 0; col != matrix[0].size(); col++) {
if (matrix[row][col] == 'O') continue;
bool ok = true;
for (auto neighbour : neighbours) {
auto move = test_move(matrix, vector<int>{row, col}, neighbour);
if (move.empty()) continue;
if (matrix[move[0]][move[1]] == 'X') {
ok = false;
break;
}
}
if (ok) cout << row << ", " << col << endl;
}
}
cout << test_move_calls << " checks made" << endl;
}
|