So, I have an assignment to do in C++, but I can't figure out how to :/
I have to use backtracking, which is completely alien to me.
The problem:
You are given n number of people with their names and m number of relations(friends) between them. Each relation consists of two names, forming a pair of strings. Everyone has at least n/2 friends. The first person has a book which he lends to one of his friends.
The book has to "travel" through everyone and end up in the hands of the first person.
Write out every possible solution.
The input should contain a number for n followed by a number for m followed by m number of rows each containing a relation.
Something like this:
The input...
3 3
Matt Susy
Matt Alan
Susy Alan
...would result in:
Matt Susy Alan Matt
Matt Alan Susy Matt
and so on for more people.
I have been banging my head against the table because no matter what I do, I can't figure it out :/
As I said, this is a relatively new concept for me and every little help would be appreciated :)
The thing is that this code works, but I can't submit it, because the teacher said that someone already approached this problem using this method, so I have to come up with a different solution for it. (Believe it or not, this has happened too many times already...)
The base idea can have some resemblance to this one, but a different approach would be better.
Please forgive the names of the variables, english is not my first language so I wrote it using words from my language :)
#include<iostream>
#include<fstream>
using namespace std;
void input_1D(string nev_1D[])
{
ifstream be("skam0286_L6_9.txt");
int x, i, j, k=1, ok;
string akt;
be>>x>>x;
for(i=0; i<2*x; ++i)
{
be>>akt;
for(j=1, ok=1; j<=k; ++j) if(nev_1D[j]==akt) ok=0;
if(ok) nev_1D[k++]=akt;
}
be.close();
}
void input_2D(int &n, int &m, string nev_2D[][3])
{
int i;
ifstream be("skam0286_L6_9.txt");
be>>n>>m;
for(i=1;i<=m;++i) be>>nev_2D[i][1]>>nev_2D[i][2];
be.close();
}
void output(int n, string eredm[])
{
int i;
for(i=1;i<=n;i++) cout<<eredm[i]<<" ";
cout<<endl;
}
bool helyes(int n, string akt, string eredm[])
{
int i, ok=0;
for(i=1; i<n && !ok; ++i)
if(eredm[i]==akt) ok=1;
return ok;
}
bool barat(int m, string nev_2D[][3], string n1, string n2)
{
int i, ok=0;
for(i=1;i<=m && !ok; ++i)
if((n1==nev_2D[i][1] && n2==nev_2D[i][2])||(n1==nev_2D[i][2] && n2==nev_2D[i][1]))
ok=1;
return ok;
}
void bt(int n, int m, string nev_2D[][3], string nev_1D[], string eredm[], int ind)
{
if(ind==n+1){
if(barat(m, nev_2D, eredm[1], eredm[ind-1])){
eredm[ind]=eredm[1];
output(ind, eredm);
}
}
else{
int i;
for(i=1; i<=n; ++i)
if(!helyes(ind, nev_1D[i], eredm) && barat(m, nev_2D, eredm[ind-1], nev_1D[i])){
eredm[ind]=nev_1D[i];
bt(n, m, nev_2D, nev_1D, eredm, ind+1);
}
}
}
int main()
{
int n, m;
string nev_2D[100][3], nev_1D[100], eredm[100];
eredm[1]="Matt";
input_2D(n,m,nev_2D);
input_1D(nev_1D);
bt(n, m, nev_2D, nev_1D, eredm, 2);
return 0;
}