Buon giorno a tutti! ho scritto questo codice per un esercizio, ma non capisco perchè non funziona correttamente..need help!! grazie!!
testo dell'esercizio ->
https://dl.dropboxusercontent.com/u/32518736/Esercizio1_25-02.pdf
questi i dati in input
5 10
1 0 0 0 1 0 0 0 0 1
0 0 0 1 1 0 0 0 1 0
1 0 1 0 1 0 0 1 0 0
1 0 0 0 1 0 1 0 0 0
1 0 0 1 0 1 0 0 0 0
il mio codice
#include<iostream>
#include <fstream>
#include <climits>
using namespace std;
void stampa(int * x,int lim2,ofstream & OUT)
{
for(int i=0;i<lim2;i++)
{OUT<<x[i]<<" ";}
OUT<<endl;
}
bool check_paths(bool * X,int * x,int lim2,int &lim3,int i,int k)
{
if(lim2==0)
{return true;}
else
{
if(i==0)
{
if(X[k*lim3+i]==true)
{x[k]=i;check_paths(X,x,lim2-1,lim3,i,k+1);}
if(X[k*lim3+i+1]==true)
{x[k]=i+1;check_paths(X,x,lim2-1,lim3,i+1,k+1);}
else return false;
}
if(i==lim3-1)
{
if(X[k*lim3+i-1]==true)
{x[k]=i-1;check_paths(X,x,lim2-1,lim3,i-1,k+1);}
if(X[k*lim3+i]==true)
{x[k]=i;check_paths(X,x,lim2-1,lim3,i,k+1);}
}
if(i!=0 && i!=lim3-1)
{if(X[k*lim3+i]==true)
{x[k]=i;check_paths(X,x,lim2-1,lim3,i,k+1);}
if(X[k*lim3+i-1]==true)
{x[k]=i-1;check_paths(X,x,lim2-1,lim3,i-1,k+1);}
if(X[k*lim3+i+1]==true)
{x[k]=i+1;check_paths(X,x,lim2-1,lim3,i+1,k+1);}
}
}
}
bool find_paths(bool * X,int &lim2,int &lim3,int * x,ofstream & OUT,int dim,int i,int k)
{
if(dim==0)
{return false;}
else
{
if(X[i]==true)
{
x[k]=i;
if(check_paths(X,x,lim2,lim3,i,k+1))
{
stampa(x,lim2,OUT);
return true;
}
find_paths(X,lim2,lim3,x,OUT,dim-1,i+1,k);
}
else
{
find_paths(X,lim2,lim3,x,OUT,dim-1,i+1,k);
}
}
}
main()
{
ifstream INP("input");
ofstream OUT("output");
if(INP && OUT)
{
int lim2, lim3;
INP>>lim2>>lim3;
bool* M=new bool[lim2*lim3];
int* path=new int[lim2];
int dim=lim3;int i=0;int k=0;
for(int i=0; i<lim2*lim3; i++)
INP>>M[i];
bool x=find_paths(M,lim2,lim3,path,OUT,dim,i,k); // da fare
if(!x)
OUT<<"nessun cammino"<<endl;
INP.close();
OUT.close();
}
else
cout<<"problemi con i file";
}