1

We have a 2D array which is NxN in size .We choose any row and shift it left or right . Like if a row is "3 4 5 6" we can shift this by 1 unit to right to get "6 3 4 5" . So I have to print all possible 2D array from a given array using row shift shown as above.

It gets very tricky combination , I tried iteratively but it became too complex for me so I think it should be easier if we tackle this problem recursively , but I couldn't complete the problem anyways . I have no problem in shift but i run into problem at generating all possible combinations .

This is what I found relating but couldn't apply to my own problem.

Community
  • 1
  • 1
Manas Verma
  • 432
  • 1
  • 3
  • 12

1 Answers1

1

This seems like something you could do with a nested for loop. Assuming you have a function that can shift a row in your 2D array called rowShift( rowNum, rowOffset, &theArray )...

int i1,i2,i3,i4;
for(i4=0; i4 <= 3; ++i4) {
    for(i3=0; i3 <= 3; ++i3) {
        for(i2=0; i2 <= 3; ++i2) {
            for(i1=0; i1 <= 3; ++i1) {
                rowShift(0, i1, theArray);
            }
            rowShift(1, i2, theArray);
        }
        rowShift(2, i3, theArray);
    }
    rowShift(3, i4, theArray);
}

If you want to save every combination, you'll need some sort of saveState() function after each rowShift call in the loops.

I imagine you could also do this as a recursive function as well.

I'm curious what this is being used for - if you don't mind sharing. Seems like an interesting problem. :)

BikerJared
  • 111
  • 2
  • 8
  • Okay , i will tell you wether it works or not . Its a part of this poblem http://opc.iarcs.org.in/index.php/problems/PUZZLE taken from contests , if you like contests you can pm me , i love solving contest problems :) – Manas Verma Nov 03 '12 at 08:42