Skip to content

Instantly share code, notes, and snippets.

@bhargavkulk
Created March 1, 2022 06:38
Show Gist options
  • Save bhargavkulk/358126086423cf70c3e1adb7135055cf to your computer and use it in GitHub Desktop.
Save bhargavkulk/358126086423cf70c3e1adb7135055cf to your computer and use it in GitHub Desktop.
dsa pool question
#include <iostream>
#include <vector>
using namespace std;
using Matrix = vector<vector<int>>;
int i = 1;
void recur(Matrix &matrix, int cx, int cy, int size, int px, int py) {
if (size == 1) return;
int q1x = cx + (size / 2);
int q1y = cy + (size / 2) - 1;
int q2x = cx + (size / 2) - 1;
int q2y = cy + (size / 2) - 1;
int q3x = cx + (size / 2) - 1;
int q3y = cy + (size / 2);
int q4x = cx + (size / 2);
int q4y = cy + (size / 2);
cout << i << " " << px << ", " << py << "\n";
if (px >= q1x && py <= q1y) {
cout << "q1\n";
matrix[q2y][q2x] = i;
matrix[q3y][q3x] = i;
matrix[q4y][q4x] = i;
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
i++;
cout << "\n";
// quad 1
recur(matrix, q1x, cy, size / 2, px, py);
// quad 2
recur(matrix, cx, cy, size / 2, q2x, q2y);
// quad 3
recur(matrix, cx, q3y, size / 2, q3x, q3y);
// quad 4
recur(matrix, q4x, q4y, size / 2, q4x, q4y);
}
if (px <= q2x && py <= q2y) {
cout << "q2\n";
matrix[q1y][q1x] = i;
matrix[q3y][q3x] = i;
matrix[q4y][q4x] = i;
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
i++;
cout << "\n";
// quad 1
recur(matrix, q1x, cy, size / 2, q1x, q1y);
// quad 2
recur(matrix, cx, cy, size / 2, px, py);
// quad 3
recur(matrix, cx, q3y, size / 2, q3x, q3y);
// quad 4
recur(matrix, q4x, q4y, size / 2, q4x, q4y);
}
if (px <= q3x and py >= q3y) {
cout << "q3\n";
matrix[q2y][q2x] = i;
matrix[q1y][q1x] = i;
matrix[q4y][q4x] = i;
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
i++;
cout << "\n";
// quad 1
recur(matrix, q1x, cy, size / 2, q1x, q1y);
// quad 2
recur(matrix, cx, cy, size / 2, q2x, q2y);
// quad 3
recur(matrix, cx, q3y, size / 2, px, py);
// quad 4
recur(matrix, q4x, q4y, size / 2, q4x, q4y);
}
if (px >= q4x and py >= q4y) {
cout << "q4\n";
matrix[q2y][q2x] = i;
matrix[q3y][q3x] = i;
matrix[q1y][q1x] = i;
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
i++;
cout << "\n";
// quad 1
recur(matrix, q1x, cy, size / 2, q1x, q1y);
// quad 2
recur(matrix, cx, cy, size / 2, q2x, q2y);
// quad 3
recur(matrix, cx, q3y, size / 2, q3x, q3y);
// quad 4
recur(matrix, q4x, q4y, size / 2, px, py);
}
}
int main(int argc, char const *argv[]) {
int n;
int px;
int py;
cin >> n >> px >> py;
px--;
py--;
Matrix matrix = Matrix();
for (int i = 0; i < n; i++) {
matrix.push_back(vector<int>(n, 0));
}
matrix[py][px] = -1;
cout << "input:\n";
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
recur(matrix, 0, 0, n, px, py);
cout << "\noutput:\n";
for (auto vec : matrix) {
for (auto i : vec) {
cout << i << " ";
}
cout << "\n";
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment