Numbers from 1 to 33 are written in 64 cells of the 8 x 8 square. Finished code, need to be redone.

Hello everyone, I have an assignment which is as follows:
"Write a program that will find a path in a numeric maze according to the specified rules.
Numbers from 1 to 33 are written in 64 cells of the 8 x 8 square. It is necessary, starting with the number 1 in the upper RIGHT corner, to draw a broken line that will not cross itself and will consist of vertical and horizontal sections. The polyline must pass through exactly 33 numbers and end in the lower LEFT corner at the number 33. The polyline can only move horizontally or vertically, but not diagonally.

An example of the task execution is shown in the figure by the link: https://ibb.co/LgJQMWs

I have a ready code, but it gives me "no solutions" in the output, it's needed to be redone (would be very grateful for your help):

#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>

using namespace std;

const int Size = 8, Goal = 33;

struct Position {
int row = 0, col = 0;
Position() = default;
Position(int r, int c) : row(r), col(c) { }
bool operator==(const Position& p) const { return row == p.row && col == p.col; }
friend ostream& operator<<(ostream& out, const Position& p) {
return out << '(' << p.col << ',' << p.row << ')';
}
};

bool solve(int grid[][Size+2], vector<bool>& b, Position p, vector<Position>& path) {
int val = grid[p.row][p.col];
if (val == -1 || b[val]) return false;
b[val] = true;
if (val == Goal) {
if (find(b.begin(), b.end(), false) == b.end()) {
path.push_back(p);
return true;
}
}
else if (solve(grid, b, Position(p.row+1, p.col), path) ||
solve(grid, b, Position(p.row-1, p.col), path) ||
solve(grid, b, Position(p.row, p.col+1), path) ||
solve(grid, b, Position(p.row, p.col-1), path)) {
path.push_back(p);
return true;
}
b[val] = false;
return false;
}

void print_grid(const int grid[][Size+2], const vector<Position>& path) {
for (int r = 1; r <= Size; ++r) {
for (int c = 1; c <= Size; ++c) {
cout << setw(2) << grid[r][c];
// finding in vector adequate for small paths; otherwise a map should be used
if (auto it = find(path.begin(), path.end(), Position(r, c)); it != path.end())
cout << '(' << setw(2) << path.size() - distance(path.begin(), it) << ") ";
else
cout << " ";

}
cout << '\n';
}
}

void print_path_dirs(const vector<Position>& path) {
for (size_t i = path.size(); i-- > 1; )
if (path[i].row != path[i-1].row)
cout << (path[i].row < path[i-1].row ? 'D' : 'U');
else
cout << (path[i].col < path[i-1].col ? 'R' : 'L');
cout << '\n';
}

int main() {
int grid[Size+2][Size+2] = {
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, 1, 21, 9, 25, 20, 5, 16, 1, -1},
{-1, 32, 12, 17, 11, 26, 27, 10, 18, -1},
{-1, 20, 27, 6, 8, 29, 15, 11, 32, -1},
{-1, 2, 31, 28, 30, 24, 13, 4, 17, -1},
{-1, 13, 22, 28, 4, 26, 2, 10, 25, -1},
{-1, 6, 19, 31, 15, 8, 30, 14, 5, -1},
{-1, 19, 18, 22, 29, 16, 24, 7, 23, -1},
{-1, 33, 23, 14, 7, 3, 9, 12, 3, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
};
vector<bool> b(34);
b[0] = true;
vector<Position> path;

if (solve(grid, b, Position(1, 1), path)) {
print_grid(grid, path);
cout << '\n';
print_path_dirs(path);
}
else
cout << "no solution\n";
}

Last edited on
sergeyutkin221,
You are already using one of several codes given to you for your original assignment:
http://www.cplusplus.com/forum/general/278578/#msg1202750
You have clearly not made not even the slightest effort to understand it enough to make the really minimal change needed for your new problem.

Make some effort of your own.
Last edited on
I agree with you, but the code isn't mine, I don't understand it a little, maybe it will be clearer for you what needs to be changed here so that the broken line goes not from the upper left corner, but from the upper right and to the lower left.
I changed the arrangement of the numbers, it's mirrored from my original assignment.
Topic archived. No new replies allowed.