0% found this document useful (0 votes)
79 views

Backtracking Algorithm

This C++ program uses recursion to count the number of possible paths through a graph given the number of nodes and edges. It takes input from a file, calls a recursive function to generate all paths as strings and check each one, and outputs the path count to a separate file. The recursive function generates all possible paths as strings by iterating through 0s and 1s at each node position, and checks each result against a forbidden string input before incrementing the path counter.

Uploaded by

David Didi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
79 views

Backtracking Algorithm

This C++ program uses recursion to count the number of possible paths through a graph given the number of nodes and edges. It takes input from a file, calls a recursive function to generate all paths as strings and check each one, and outputs the path count to a separate file. The recursive function generates all possible paths as strings by iterating through 0s and 1s at each node position, and checks each result against a forbidden string input before incrementing the path counter.

Uploaded by

David Didi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include <iostream>

#include <fstream>
#include <string.h>

using namespace std;


char v[1000000000];
int t,n,m;
long long int counter=0;
char s[1100];
int Verificare(){
if(strstr(v,s)==NULL)
return 1;
return 0;
}
void RecBk(int k){
for(int i=0;i<=1;i++){
int p[1];
p[0]=char(48+i);
v[k]=p[0];
if(k==n-1){
v[n]='\0';
if(Verificare()){
counter++;
}
}else{
RecBk(k+1);
}

}
}
int main(){
fstream f("traseu.in",ios::in);
fstream g("traseu.out",ios::out);
f>>t;
int o=t;
while(t!=0){
f>>n;
f>>m;
cin.get(s,100);
cin.get();
RecBk(0);
g<<"Case #"<<o-t+1<<": "<<counter<<endl;
counter=0;
t--;
}
g.close();
f.close();
system("pause");
}

You might also like