I spent last nights trying to rewrite the following code using cycles...but wihout success :( Thanks, Ozzman
// Permutation.cpp : Defines the entry point for the console application.
//
#include <iostream>
#include <conio.h>
#include <windows.h>
#include "stdafx.h"
#define N 3
int level = -1;
int List[N];
int iteration = 0;
struct sList {
int value;
int available;
};
#define K 4
sList aList[K];
void visit(void)
{
level ++;
if (level == N){
//iteration++;
//printf("%d.", iteration);
for(int j = 0; j < N; j++){
printf("%d ",List[j]);
}
printf("\n");
}
else {
for (int i = 0; i < K; i++){
if(aList[i].available > 0){
List[level] = aList[i].value;
aList[i].available--;
visit();
aList[i].available++;
List[level] = 0;
}
}
}
level--;
}
int _tmain(int argc, _TCHAR* argv[])
{
aList[0].value = 1;
aList[0].available = 1;
aList[1].value = 2;
aList[1].available = 1;
aList[2].value = 3;
aList[2].available = 1;
aList[3].value = -1;
aList[3].available = N;
visit();
//getch();
return 0;
}