Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- struct nodo{int info; nodo* next;
- nodo(int a=0, nodo* b=0){
- info=a;
- next=b;
- }
- };
- #include<iostream>
- using namespace std;
- bool trova_lab(int *L, int n, int i, int j){ //ho L=array, dim=n iniziale, i=i, j=casella bianca prima riga
- /*
- for(int k=0;k<n;k++){
- for(int h=0;h<n;h++){
- cout<<*(L+(k*n)+h)<<' ';
- }
- cout<<endl;
- }*/
- if(i<n-1){
- if(j==0){
- if(*(L+(i+1)*n+j)==1){
- // cout<<"giu' dritto"<<endl;
- return trova_lab(L, n, i+1, j);
- }
- else if(*(L+(i+1)*n+(j+1))==1){
- // cout<<"giu' a dx"<<endl;
- return trova_lab(L, n, i+1, j+1);
- }
- else if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j+1))==0)){
- // cout<<"tutti zeri 1"<<endl;
- return false;
- }
- }
- else if(j==n-1){
- if(*(L+(i+1)*n+j)==1){
- // cout<<"giu' dritto"<<endl;
- return trova_lab(L, n, i+1, j);
- }
- else if (*(L+(i+1)*n+(j-1))==1){
- // cout<<"giu' a sx"<<endl;
- return trova_lab(L, n, i+1, j-1);
- }
- else if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j-1))==0)){
- // cout<<"tutti zeri 2" <<endl;
- return false;
- };
- }
- else if (j!=0 && j!=n-1){
- if(*(L+(i+1)*n+j)==1){
- // cout<<"giu' dritto"<<endl;
- return trova_lab(L, n, i+1, j);
- }
- else if(*(L+(i+1)*n+(j+1))==1){
- // cout<<"giu' a dx"<<endl;
- return trova_lab(L, n, i+1, j+1);
- }
- else if (*(L+(i+1)*n+(j-1))==1){
- // cout<<"giu' a sx"<<endl;
- return trova_lab(L,n, i+1, j-1);
- };
- if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j+1))==0)&&(*(L+(i+1)*n+(j-1))==0)){
- // cout<<"tutti zeri 3" <<endl;
- return false;
- };
- }
- //
- }
- else {//sono giunto all' ultima riga
- // cout<<"i==n-1 = "<<i<<endl;
- return true;
- }
- }
- void define(int* L, int i, int j, int n, nodo* & x){
- x=new nodo(j,0);
- if(i<n){
- if (*(L+(i+1)*n+(j-1))==1 && ((*(L+(i+2)*n+j)!=0)|| (*(L+(i+2)*n+(j+1))!=0)||(*(L+(i+2)*n+(j-1))!=0)) ){
- cout<<"giu a sx"<<endl;
- define(L, i+1, j-1,n, x->next);
- }
- else if(*(L+(i+1)*n+(j+1))==1){
- cout<<"giu a dx"<<endl;
- define(L, i+1, j+1,n,x->next);
- }
- else if(*(L+(i+1)*n+j)==1){
- cout<<"giu dritto"<<endl;
- define(L, i+1, j,n, x->next);
- }
- }
- }
- void stampa(nodo* x,int n,int i){
- if(i<n){
- cout<<'('<<i<<','<<x->info<<')';
- cout<<' ';
- stampa(x->next, n, i+1);
- }
- }
- int trova (int* L, int n, int i){ //lancia trova_lab sulle caselle bianche della prima riga
- if(i<n){
- if(L[i]==1){
- if(trova_lab(L,n, 0, i)==true){
- return i;
- }
- else
- return trova(L, n, i+1);
- }
- else {
- return trova(L, n, i+1);
- }
- }
- else
- return -1;
- }
- main()
- {
- int n;
- cin>>n;
- int* L= new int [n*n];
- for(int i=0; i<n*n; i++)
- cin>>L[i];
- int j=trova(L,n,0);//da fare
- nodo* x=NULL;
- // cout<<j<<endl;
- if(j!=-1){
- define(L,0,j,n,x);}
- cout<<"start"<<endl;
- if(x)
- { cout<<"il cammino e':"; stampa(x,n,0);}//da fare
- else
- cout<<"il cammino non esiste"<<endl;
- cout<<"end"<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement