Maze with exact moves available to finish

Hi, I'd like to ask for help. I neeed to print maze made of '.' and '#' where '#' is wall and '.' is just place I can move at.

Maze has to be generated with exact rows and columns depending on the users input, but I've already done that with one-dimensional array (but I'm still not sure if it woud work or I will have to do that with two-dimensional array).

Start is at the upper left-hand corner and the end is at the lower right-hand corner.
Variable k has to be at the interval I've written (lowk and highk)

I need to find and write algorithm that will successfully lead you from start to finish at the exact moves (variable k)=> path will be printed from '.' and that will write '#' (walls) so that number of moves is equal to variable k.
This number of moves cannot exceed or be lower than k.



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
#include <iostream>
#include <math.h>

using namespace std;

int main()
{
    int wardens, rows, columns, k, lowk, highk, columner;
    columner=0;
    cout << "Enter number of wardens (from 1 to 1000)" << endl;
    cin >> wardens;
    for (int i=1;i<=wardens;i++)
    {
        lowk=0;
        highk=0;
        cout << "Dimensions of " << i << ". warden:" << endl;
        cout << "Enter number of rows (from 1 to 75)(1 <= rows <= 75)" << endl; //rows
        cin >> rows;
        cout << "Enter number of columns (from 1 to 75)(1 <= columns <= 75)" << endl; //columns
        cin >> columns;
        lowk=columns+rows-1;
        highk=2*(((rows-1)/4)*((columns-1)%4)+((columns-1)/4)*(rows-1))+rows+columns-1;
        cout << "Enter k (from " << lowk << " to "<< highk << ")(" << lowk << " <= k <= " << highk << ")" << endl; // distance/lenght of shortest way/number of moves
        cin >> k;
        char array[rows*n];
        for (int r=0;r<(rows*columns);r++)
        {
            array[r]='.';
            if (array[r]==array[0])
                array[r]='.'; //first position will be always '.'
            else if (array[r]==array[(rows*columns)-1])
                array[r]='.'; //last position will be always '.'

            cout << array [r] << " ";
            columner++;
            if (columner%columns==0) // to have X columns at Y rows
                cout << endl;
        }
    }

    return 0;
}



if in input:
wardens=(any)
rows=5
columns=6
k=12
output:

......
......
......
......
......


desired output example:

......
..##..
.#....
.##.##
#.....
Last edited on
I'd use a 2D array for sure. Let the compiler do the arithmetic for you.

Line 25: n is undefined so this doesn't compile. When posting code and results, please be sure that the code generates the results.

Lines 26-31:
The first time through the loop, r==0, so line 28 sets array[0]='.'. Line 29 is true so line 30 also sets array[0]='.'.

The second time through the loop, r==1. Line 28 sets array[1]='.'. Line 29 compares array[1] to array[0] and since they're both '.', it's true and line 30 sets array[1]='.' again.

The pattern repeats. For each value of r, line 28 sets array[r]='.' and the comparison at line 29 is true.

You desired output contains '#' characters, but I don't see anything in the code that would put '#' in any location.
there's supposed to be variable columns instead of n, im sorry for that.

I know that this code will print only '.' but that's just because I dont know how write the algorithm that will print number of '#' correctly so that shortest way made of '.' will match the value of variable k. Im trying to figure it out how could it work.
Topic archived. No new replies allowed.