#include <cstdio>
#include <iostream>
using namespace std;
int src[100], out[100], used[100], n;
void btrack( int k ) {
if (k>=n) {
for (int i=0 ; i<n ; i++) {
printf(" %d ",out[i]);
}
printf("\n");
return;
}
for (int i=0 ; i<n ; i++) {
if (!used[i]) {
used[i] = 1;
out[k] = src[i];
btrack( k+1 );
used[i] = 0;
}
}
return;
}
int main ( void ) {
while ( scanf("%d",&n) && n ) {
for (int i=0 ; i<n ; i++) {
used[i] = 0;
src[i] = i+1;
}
btrack( 0 );
}
return 0;
}
Friday, June 15, 2012
Backtracking Basic Code
Subscribe to:
Post Comments (Atom)
Connect Rapoo MT750S with Linux (Tested on Manjaro)
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
I like coding a lot, keeps me glued to the PC for hours. For that reason it's a need to edit the Syntax Highlighter to suit my eyes for...
-
I bought this obvious copy of MX Master 2S in hopes of having the device switching functionality along with a lightweight body because I ha...
-
Method: The problem at first glance seems too straightforward but it's not that much. Think a bit about the lines "Erin can add ...
i dont understand how "if (!used[i])" work...
ReplyDeletecould u plz telling me how it work.. Tafhim bro..
i dont understand how "if (!used[i])" work.. plz tell me..
ReplyDelete