Guest User

Untitled

a guest
Jan 16th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int min1,n,m;
  4. char A[51][51]; // for storing the matrix
  5. bool B[51][51]; //to keep track which cell is visted (a cell will be set to true when visited)
  6. void coins(int i,int j,int k,int count,int steps)
  7. {
  8. if(steps >k || count>=min1) //Case when the chosen path takes more time than k
  9. {
  10. return;
  11. }
  12. else if(A[i][j]=='*') // when goal is found
  13. {
  14.  
  15. if(count<min1)
  16. min1=count;
  17.  
  18. return;
  19. }
  20. else if(B[i][j]==true) // when a cell is found which has already been visited(in case of cycles)
  21. return;
  22. else // this else part tries to get all possible paths for a cell A[i][j]
  23. {
  24.  
  25. if(A[i][j]=='U')
  26. {
  27. B[i][j]=true;
  28. if((i-1)>=0)
  29. coins(i-1,j,k,count,steps+1);
  30.  
  31. if((j-1)>=0)
  32. {
  33. char old=A[i][j];
  34. A[i][j]='L';
  35. coins(i,j-1,k,count+1,steps+1);
  36. A[i][j]=old;
  37. }
  38.  
  39. if((i+1)<n)
  40. {
  41. char old=A[i][j];
  42. A[i][j]='D';
  43. coins(i+1,j,k,count+1,steps+1);
  44. A[i][j]=old;
  45. }
  46.  
  47. if((j+1)<m)
  48. {
  49. char old=A[i][j];
  50. A[i][j]='R';
  51. coins(i,j+1,k,count+1,steps+1);
  52. A[i][j]=old;
  53. }
  54. B[i][j]=false;
  55.  
  56. }
  57. else if(A[i][j]=='L')
  58. {
  59. B[i][j]=true;
  60. if((j-1)>=0)
  61. coins(i,j-1,k,count,steps+1);
  62.  
  63. if((i-1)>=0)
  64. {
  65. char old=A[i][j];
  66. A[i][j]='U';
  67. coins(i-1,j,k,count+1,steps+1);
  68. A[i][j]=old;
  69. }
  70.  
  71. if((i+1)<n)
  72. {
  73. char old=A[i][j];
  74. A[i][j]='D';
  75. coins(i+1,j,k,count+1,steps+1);
  76. A[i][j]=old;
  77. }
  78.  
  79. if((j+1)<m)
  80. {
  81. char old=A[i][j];
  82. A[i][j]='R';
  83. coins(i,j+1,k,count+1,steps+1);
  84. A[i][j]=old;
  85. }
  86. B[i][j]=false;
  87.  
  88. }
  89. else if(A[i][j]=='D')
  90. {
  91. B[i][j]=true;
  92. if((i+1)<n)
  93. coins(i+1,j,k,count,steps+1);
  94.  
  95. if((i-1)>=0)
  96. {
  97. char old=A[i][j];
  98. A[i][j]='U';
  99. coins(i-1,j,k,count+1,steps+1);
  100. A[i][j]=old;
  101. }
  102.  
  103. if((j-1)>=0)
  104. {
  105. char old=A[i][j];
  106. A[i][j]='L';
  107. coins(i,j-1,k,count+1,steps+1);
  108. A[i][j]=old;
  109. }
  110.  
  111. if((j+1)<m)
  112. {
  113. char old=A[i][j];
  114. A[i][j]='R';
  115. coins(i,j+1,k,count+1,steps+1);
  116. A[i][j]=old;
  117. }
  118. B[i][j]=false;
  119.  
  120. }
  121. else if(A[i][j]=='R')
  122. {
  123. B[i][j]=true;
  124. if((j+1)<m)
  125. coins(i,j+1,k,count,steps+1);
  126.  
  127. if((i-1)>=0)
  128. {
  129. char old=A[i][j];
  130. A[i][j]='U';
  131. coins(i-1,j,k,count+1,steps+1);
  132. A[i][j]=old;
  133. }
  134.  
  135. if((j-1)>=0)
  136. {
  137. char old=A[i][j];
  138. A[i][j]='L';
  139. coins(i,j-1,k,count+1,steps+1);
  140. A[i][j]=old;
  141. }
  142. if((i+1)<n)
  143. {
  144. char old=A[i][j];
  145. A[i][j]='D';
  146. coins(i+1,j,k,count+1,steps+1);
  147. A[i][j]=old;
  148. }
  149. B[i][j]=false;
  150.  
  151. }
  152. }
  153. }
  154. int main()
  155. {
  156. int k,i,j;
  157. cin>>n>>m>>k;
  158.  
  159.  
  160. for(i=0;i<n;++i)
  161. for(j=0;j<m;++j)
  162. cin>>A[i][j];
  163.  
  164. min1=1000000000;
  165.  
  166. coins(0,0,k,0,0);
  167. if(min1==1000000000)
  168. cout<<-1;
  169. else
  170. cout<<min1;
  171. }
Add Comment
Please, Sign In to add comment