-1

Thanks in advance for your time :). When I tried to implement NQueens, I kinda observed a case like...instance of the variables after recursion would usually be unfolding itself in a stack manner right, so does in my case with all the variable except one 2-D array. It isn't being unfolded..instead staying the way it was at the last of the recursive call...appreciate for taking time to help, thanks =D.

Place of issue...the variable "board[100][100] " is not unfolding instead, like said, was staying at the last instance of recursive call I expect it to be like 1st queen placed, 2nd queen placed, 3rd queen placed and then 3rd queen placed, 2nd queen placed, 1st queen placed just like the placed variable. Or I guess maybe that's how it is supposed to be working, however, I want to know the logical explanation on it's behaviour on why the "board" variable isn't unfolding.

void foo(bool board[100][100], int x, int y, int n, int m, int placed)
{
    if( placed == n )
    {
        display(board, n, m);

        return ;
    }

    int i, j;

    for(i=x; i<=n; i++)
    {
        for(j=y; j<=m; j++)
        {
            if( checkBoard(board, n, m, i, j) )
            {
                board[i][j] = true;

                display(board, n, m);
                cout<<placed;

                foo(board, 1, 1, n, m, placed+1);

                display(board, n, m);
                cout<<placed;
            }
        }
    }
}

My Output -> As you can see the variable named "placed" is unfolding like expected, but it isn't in the case of this 2-D array called "board[100][100]".

Whole Code:-

#include <bits/stdc++.h>

using namespace std;

int display(bool board[100][100], int n, int m)
{
    int i, j;

    cout<<"\n===============================================\n\n";

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            if( board[i][j] )
            {
                cout<<"|Q|";
            }

            else
            {
                cout<<"|_|";
            }
        }

        cout<<'\n';
    }

    cout<<"\n===============================================\n";
}

bool checkBoard(bool board[100][100], int n, int m, int x, int y)
{
    int i, j;

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            if( board[i][j] and ( (x==i or y==j) or (abs(x-i)==abs(y-j)) ) )
            {
                return false;
            }
        }
    }

    return true;
}

void foo(bool board[100][100], int x, int y, int n, int m, int placed)
{
    if( placed == n )
    {
        display(board, n, m);

        return ;
    }

    int i, j;

    for(i=x; i<=n; i++)
    {
        for(j=y; j<=m; j++)
        {
            if( checkBoard(board, n, m, i, j) )
            {
                board[i][j] = true;

                display(board, n, m);
                cout<<placed;

                foo(board, 1, 1, n, m, placed+1);

                display(board, n, m);
                cout<<placed;
            }
        }
    }
}

int main(void)
{
    int i, j,
        n = 4,
        m = 4;

    bool board[100][100];

    for(i=1; i<=n; i++)
    {
        for(j=1; j<=m; j++)
        {
            board[i][j] = false;
        }
    }

    foo(board, 1, 1, n, m, 0);

    return 0;
}
Vineeth
  • 11
  • 4
  • Are you expecting `board` content to be copied when invoking this function or something? – user7860670 Apr 19 '20 at 04:24
  • @user7860670, I expect it to be like 1st queen place, 2nd queen placed, 3rd queen placed and then 3rd queen placed, 2nd queen placed, 1st queen placed just like the `placed` variable – Vineeth Apr 19 '20 at 04:27
  • You show-case lots of bad habits in your code. First there's the one-based array indexing (C++ uses zero-based indexing). Then please [don't include ``](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). And please [don't do `using namespae std;`](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). Oh and try to use semantically named variable-names instead of just single-letter names. And comments about what your program is doing, and why. – Some programmer dude Apr 19 '20 at 04:28
  • @Someprogrammerdude Oh, I usually work with 0-index however since Im new to recursion I thought it would be easy to get a better idea on how this work with 1-index. And I don't see a reason why not to include ` – Vineeth Apr 19 '20 at 04:36
  • @Someprogrammerdude Thanks for including those hyperlinks...I didnt notice them earlier... – Vineeth Apr 19 '20 at 05:19

1 Answers1

0

For the future me or whoever is stuck with the same problem, it's that array is not just an array of variables, it's more like a pointer pointing to a variable(memory block in memory palace). So which recursion, the variables would be roll backed to their previous values after successful recursion call in a stack manner however these arrays or pointers do not since, arrays or pointers are merely pointing. So the values at the memory block would the same unless changed explicitly/manually but cannot be unfolded/roll backed to previous instances while unfolding recursion.

This code that I wrote a hour ago helped me understand it, hopefully it helps others...

#include <bits/stdc++.h>

using namespace std;

void display(int a[5][5], int b[5])
{
    int i, j;

    cout<<"\n===============================================\n";

    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            cout<<a[i][j]<<' ';
        }

        cout<<'\n';
    }

    cout<<"\n-----------------------------------------------\n";

    for(i=0; i<5; i++)
    {

        cout<<b[i]<<' ';
    }

    cout<<"\n===============================================\n";
}

void foo(int i, int j, int a[5][5], int b[5], int var, int *temp)
{
    if( i == 5 )
    {
        return ;
    }

    a[i][j] = b[i] = 1;

    display(a, b);
    cout<<var<<' '<<*temp;

    foo(i+1, j+1, a, b, var++, &(++*temp));

    display(a, b);
    cout<<var<<' '<<*temp;
}

int main(void)
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    int i, j, a[5][5], b[5], *temp, alpha;

    temp = &(alpha = 0);

    for(i=0; i<5; i++)
    {
        for(j=0; j<5; j++)
        {
            a[i][j] = 0;
        }

        b[i] = 0;
    }

    foo(0, 0, a, b, 0, temp);

    return 0;
}

Also, note that foo(i+1, j+1, a, b, var++, &(++*temp)); here *temp should be pre-incremented but not post since, *temp would already be assigned and can't be updated for some unknown reason....All of these are based on my intuition and several experiments without proper research and probably might all be wrong to actual explanation, so please feel free to correct me.

Vineeth
  • 11
  • 4