The task is to make a program that prints out how many possible paths a driver can use to visit three cities so that he visits each city exactly n times. The program must be executed within 1 minute and print the solution for n = 10. The program works correctly but for n above 7 it doesn't execute in time. Is there a way to get it to execute n=10 in time? These are some of the solutions: n = 1, the solution is 6, n = 2, the solution is 30, n = 3, the solution is 174 and n = 4, the solution is 1092.
#include<iostream>
#include <algorithm>
using namespace std;
double paths (long long unsigned int path[],int n,double s,int e){
int r=0;
do{
for(int i=0;i<e-1;i++){
if(path[i]!=path[i+1]){
r++;
}
}
if(r==e-1){
s++;
}
r=0;
}while(next_permutation(path, (path+e)));
return s;
}
int main(){
double s=0;
int y=0;
int buff=0;
int n;
cin>>n;
int e=(n*3);
long long unsigned int path[e];
while(y!=e){
for(int t=1;t<=3;t++){
path[y]=t;
y++;
}
};
bool test=false;
for(int z=0;z<e;z++){
for (int j =0; j<e; j++){
if (path[j+1] < path[j]){
buff=path[j];
path[j]=path[j+1];
path[j+1]=buff;
test=true;
}
}
if(!test){
break;
}else{
test=false;
}
}
cout<<"Number of posibilities: "<<paths(path,n,s,e)<<endl;
}