I don't know if this is the correct place to post this. Anyways, i am trying to solve this problem hosted on acmicpc live archives and getting WA.
Please point out the flaw in my logic. I am doing a dfs and storing all the paths in string vector.
http://acmicpc-live-archive.uva.es/nuevoportal/data/problem.php?p=2004
Problem code-2004
Problem name-Fire Trucks
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
using namespace std;
vector<string>out;
bool check(int i,string path){
int s=path.length();
for ( int t=0;t<s;++t)
if ( path[t]-'0'==i)
return false;
return true;
}
void dfs(int ar[21][21],int root,int final,string path){
char ch=root+'0';
path=path+ch;
if ( root==final){
out.push_back(path);
return ;
}
for ( int i=1;i<21;++i){
string tmp(path);
if ( ar[root][i]==1 && check(i,path))
dfs(ar,i,final,tmp);
}
}
int main(){
int final;
scanf("%d",&final);
int ar[21][21]={0};
int a,b;
while(scanf("%d %d",&a,&b),a+b>0){
ar[a][b]=1;
ar[b][a]=1;
}
string path="";
path.reserve(20);
dfs(ar,1,final,path);
int outs=out.size();
sort(out.begin(),out.end());
for ( int i=0;i<outs;++i){
int ws=out[i].length();
for ( int j=0;j<ws;++j)
printf("%c ",out[i][j]);
printf("\n");
}
printf("\n");
return 0;
}