Advertisement
Guest User

Untitled

a guest
May 26th, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. struct nodo{int info; nodo* next;
  4. nodo(int a=0, nodo* b=0){
  5. info=a;
  6. next=b;
  7. }
  8.  
  9. };
  10.  
  11.  
  12.  
  13.  
  14. #include<iostream>
  15.  
  16. using namespace std;
  17. bool trova_lab(int *L, int n, int i, int j){ //ho L=array, dim=n iniziale, i=i, j=casella bianca prima riga
  18.  
  19. /*
  20. for(int k=0;k<n;k++){
  21. for(int h=0;h<n;h++){
  22. cout<<*(L+(k*n)+h)<<' ';
  23. }
  24. cout<<endl;
  25. }*/
  26.  
  27. if(i<n-1){
  28.  
  29. if(j==0){
  30. if(*(L+(i+1)*n+j)==1){
  31. // cout<<"giu' dritto"<<endl;
  32.  
  33. return trova_lab(L, n, i+1, j);
  34. }
  35.  
  36. else if(*(L+(i+1)*n+(j+1))==1){
  37. // cout<<"giu' a dx"<<endl;
  38. return trova_lab(L, n, i+1, j+1);
  39. }
  40.  
  41.  
  42. else if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j+1))==0)){
  43. // cout<<"tutti zeri 1"<<endl;
  44. return false;
  45.  
  46. }
  47. }
  48.  
  49.  
  50. else if(j==n-1){
  51. if(*(L+(i+1)*n+j)==1){
  52. // cout<<"giu' dritto"<<endl;
  53. return trova_lab(L, n, i+1, j);
  54. }
  55. else if (*(L+(i+1)*n+(j-1))==1){
  56. // cout<<"giu' a sx"<<endl;
  57. return trova_lab(L, n, i+1, j-1);
  58. }
  59.  
  60. else if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j-1))==0)){
  61. // cout<<"tutti zeri 2" <<endl;
  62. return false;
  63. };
  64.  
  65. }
  66.  
  67. else if (j!=0 && j!=n-1){
  68. if(*(L+(i+1)*n+j)==1){
  69. // cout<<"giu' dritto"<<endl;
  70. return trova_lab(L, n, i+1, j);
  71. }
  72.  
  73. else if(*(L+(i+1)*n+(j+1))==1){
  74. // cout<<"giu' a dx"<<endl;
  75. return trova_lab(L, n, i+1, j+1);
  76. }
  77.  
  78. else if (*(L+(i+1)*n+(j-1))==1){
  79. // cout<<"giu' a sx"<<endl;
  80. return trova_lab(L,n, i+1, j-1);
  81. };
  82.  
  83.  
  84. if((*(L+(i+1)*n+j)==0)&& (*(L+(i+1)*n+(j+1))==0)&&(*(L+(i+1)*n+(j-1))==0)){
  85. // cout<<"tutti zeri 3" <<endl;
  86. return false;
  87. };
  88. }
  89. //
  90. }
  91.  
  92.  
  93. else {//sono giunto all' ultima riga
  94. // cout<<"i==n-1 = "<<i<<endl;
  95. return true;
  96. }
  97.  
  98. }
  99.  
  100. void define(int* L, int i, int j, int n, nodo* & x){
  101.  
  102. x=new nodo(j,0);
  103.  
  104. if(i<n){
  105. 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)) ){
  106. cout<<"giu a sx"<<endl;
  107. define(L, i+1, j-1,n, x->next);
  108. }
  109.  
  110. else if(*(L+(i+1)*n+(j+1))==1){
  111. cout<<"giu a dx"<<endl;
  112. define(L, i+1, j+1,n,x->next);
  113. }
  114.  
  115.  
  116.  
  117. else if(*(L+(i+1)*n+j)==1){
  118. cout<<"giu dritto"<<endl;
  119. define(L, i+1, j,n, x->next);
  120. }
  121.  
  122.  
  123.  
  124.  
  125. }
  126.  
  127. }
  128.  
  129. void stampa(nodo* x,int n,int i){
  130. if(i<n){
  131. cout<<'('<<i<<','<<x->info<<')';
  132. cout<<' ';
  133. stampa(x->next, n, i+1);
  134. }
  135.  
  136. }
  137.  
  138.  
  139. int trova (int* L, int n, int i){ //lancia trova_lab sulle caselle bianche della prima riga
  140. if(i<n){
  141. if(L[i]==1){
  142. if(trova_lab(L,n, 0, i)==true){
  143. return i;
  144. }
  145. else
  146. return trova(L, n, i+1);
  147. }
  148.  
  149. else {
  150. return trova(L, n, i+1);
  151. }
  152.  
  153. }
  154. else
  155. return -1;
  156. }
  157.  
  158.  
  159.  
  160. main()
  161. {
  162. int n;
  163. cin>>n;
  164. int* L= new int [n*n];
  165. for(int i=0; i<n*n; i++)
  166. cin>>L[i];
  167. int j=trova(L,n,0);//da fare
  168. nodo* x=NULL;
  169. // cout<<j<<endl;
  170. if(j!=-1){
  171. define(L,0,j,n,x);}
  172. cout<<"start"<<endl;
  173. if(x)
  174. { cout<<"il cammino e':"; stampa(x,n,0);}//da fare
  175.  
  176. else
  177. cout<<"il cammino non esiste"<<endl;
  178. cout<<"end"<<endl;
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement