Labyrinth-number

Hello everyone. So my task is to write a program that will find its way into a numerical maze.

Numbers from 1 to 33 are written in 64 cells of a square 8х8.

It is necessary, starting from the number 1 in the upper left corner, to draw a broken line that will not cross itself and will consist of vertical and horizontal sections. The broken line must pass exactly through the number 33 and end in the lower right corner at number 33. The broken line can move only horizontally or vertically, but not diagonally.

Here is my code:

#include <iostream>
#include <iomanip>
#include <vector>
#include <ctime>
#include <algorithm>
using namespace std;

int check(int n, vector<int> &a);
int move(int arr[10][10], vector<int> b, int current, int i, int j, int count);
void restart(int arr[10][10], vector<int> b);
int equal(vector<int> a, vector<int> b);

int equal(vector<int> a, vector<int> b)
{
if (a == b)
{
return 1;
}
else
return 0;
}
void restart(int arr[10][10], vector<int> b)
{
b.clear();
b.push_back(-1);
b.push_back(0);
int current = 1;
int i = 1, j = 1;
int count = 0;
move(arr, b, current, i, j, count);
}

int move(int arr[10][10], vector<int> b, int current, int i, int j, int count)
{

}

int checkfill(int n, vector<int> &a)
{
auto it = find(a.begin(), a.end(), n);
if (it != a.end())
{
return 0;
}
else
{
a.push_back(n);
return 1;
}
}

int main()
{
vector<int> a{-1, 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};
vector<int> b{-1};
int arr[10][10] = {{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
{-1, 1, 16, 5, 20, 25, 9, 21, 1, -1},
{-1, 18, 12, 27, 26, 11, 17, 12, 32, -1},
{-1, 32, 11, 15, 29, 8, 6, 27, 20, -1},
{-1, 17, 4, 13, 24, 30, 28, 31, 2, -1},
{-1, 25, 10, 2, 26, 4, 28, 22, 13, -1},
{-1, 5, 14, 30, 8, 15, 31, 19, 6, -1},
{-1, 23, 7, 24, 16, 29, 22, 18, 19, -1},
{-1, 3, 12, 9, 3, 7, 14, 23, 33, -1},
{-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}};

int k;

for (int i = 0; i < 10; i++)
{
cout << endl;
for (int j = 0; j < 10; j++)
{
cout << setw(3) << arr[i][j];
}
}
cout << endl;
restart(arr, b);

return 0;
}

What i need is function "move" that must pass from element [1] [1] of the matrix to element 33
there are any number of ways to write that. you can go across and down, down and across, or meander through the numbers...
I mean the simplest thing to do is iterate across until in the same column as 33, then iterate down until you hit it. That assumes you know where 33 is... do you?
I just need to finish the "move" function, i need your help with that.
What is this "broken line" supposed to do BETWEEN the numbers 1 and 33?

Explain your problem better.
sergeyutkin221 wrote:
Here is my code


PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.
Its a number maze from 1 to 33.
there is a time when the 'man of few words thing works'. And time when it does not.
How have you explained how to do this in your program design? (You have first produced a program design, haven't you?) If you were to do this just using pen & paper, how would you do it? How would you explain how to do it to someone who knows nothing about a maze? This is nothing to do with programming - just analysis and design. Once you have the design, then you start to translate the design into code - once small step at a time, compiling and testing after every step. At the end, assuming the design is correct, then you have a working program!
Hello sergeyutkin221,

I would agree with seeplus this is a program that needs designed first so that you know what to do.

You start out describing this a labyrinth and a maze, but either has dead-ends that restrict which way you can go. You have not properly described how to move through the maze, so right now you have 2 choices either across and down or down and then across.

And why a 10 x 10 array when it seems that an 8 x 8 array will do? And what are the (-1)s for?

I have been going over your code. The comments should explain most of what I see:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <iomanip>
#include <vector>
#include <ctime>
#include <algorithm>

using namespace std;  // <--- Best not to use.

constexpr int MAXSIZE{ 10 };  // <--- Added.
//constexpr int MAXROWS{ 8 };  // <--- These are more for future use when you have an array of different sizes.
//constexpr int MAXCOLS{ 8 };

int checkfill(int n, vector<int> &a);  // <--- Did you forget to define "check" or should this be "checkfill"
int move(int arr[][MAXSIZE], vector<int>& b, int current, int i, int j, int count);
void restart(int arr[][MAXSIZE], vector<int>& b);
int equal(vector<int>& a, vector<int>& b);

int equal(vector<int>& a, vector<int>& b)  // <--- Defined, but never called.
{
    if (a == b)  // <--- What did you intend to compare and why?
    {
        return 1;
    }
    else
        return 0;
}

void restart(int maze[][MAXSIZE], vector<int>& b)
{
    //b.clear();  // <--- These are unnecessary when all you want to do is add a (0)zero.
    //b.push_back(-1);
    b.push_back(0);

    int current = 1;   // <--- current what?
    int i = 1, j = 1;  // <--- You forgot about "p" and "q".
    int count = 0;     // <--- Count of what?

    move(maze, b, current, i, j, count);
}

int move(int maze[][MAXSIZE], vector<int>& b, int current, int i, int j, int count)
{
    return 0;  // <--- Tempory. Added because the function needs a return value so it will compile.
}

int checkfill(int n, vector<int> &a)  // <--- Defined, but never called. You could just return a "bool".
{
    auto it = find(a.begin(), a.end(), n);

    if (it != a.end())
    {
        return 0;
    }
    else
    {
        a.push_back(n);

        return 1;
    }
}

int main()
{
    vector<int> a{ -1, 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 };
    vector<int> b{ -1 };
    int maze[MAXSIZE][MAXSIZE]
    {
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
        {-1,  1, 16,  5, 20, 25,  9, 21,  1, -1},
        {-1, 18, 12, 27, 26, 11, 17, 12, 32, -1},
        {-1, 32, 11, 15, 29,  8,  6, 27, 20, -1},
        {-1, 17,  4, 13, 24, 30, 28, 31,  2, -1},
        {-1, 25, 10,  2, 26,  4, 28, 22, 13, -1},
        {-1,  5, 14, 30,  8, 15, 31, 19,  6, -1},
        {-1, 23,  7, 24, 16, 29, 22, 18, 19, -1},
        {-1,  3, 12,  9,  3,  7, 14, 23, 33, -1},
        {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1}
    };

    //int k;  // <--- Defined, but never used. ALWAYS initialize all your variables.

    for (int row{}; row < MAXSIZE; row++)  // <--- Should be in a function.
    {
        cout << '\n';

        for (int col{}; col < MAXSIZE; col++)
        {
            cout << setw(3) << maze[row][col];
        }
    }

    cout << '\n';

    restart(maze, b);

    return 0;  // <--- Not required, but makes a good break point for testing.
}

Lines 10 and 11 Could be used here, but it is more for when an array is of different dimensions.

Your forward declarations, or prototypes, are fine, but may not be necessary because you define all your functions before "main". When a function calls a function that has not yet been compiled do the forward declarations become useful.

In the "equal" function you are comparing a whole vector to a whole vector. Not sure what it is comparing, but it always seems to go to the else statement.

In "main" you define vectors "a" and "b", but I have no idea what they are or what they are used for.

A good variable name really helps. Something the describes what it is or what it is used for. Leave the single letters, "i", "j" and "k", for for loops where the variable is local to the loop and is destroyed when the loop ends. Even then a name like "row", "col" or "idx", for "index", good for a 1D array, is better than, "i", "j" and "k".

For the array the way I defined it makes it easier to visualize what it would be. The extra space before a single digit number makes no difference to the compiler, The white space is just ignored, but it does make it easier to read. And the (=) is not needed just the {}s.

That is what I see so far. And it looks like you did not design the program, but just started writing code to see what works.

Andy
the -1 boarder simplifies logic. instead of a bunch of checks against the dimensions to see if you are going out of bounds, you hit the sentinels naturally as you examine the cell data anyway.
@jonnin,

That is kind of what I figured.

But OP starts by talking about an 8 x 8 array.

On the other hand the way the array is laid out there is no real need for a 10 x 10 array and the negative numbers.

Andy
Last edited on
Topic archived. No new replies allowed.