Recursive Backtracking Search Problem

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2006
Posts: 18
Reputation: tehloki is an unknown quantity at this point 
Solved Threads: 0
tehloki tehloki is offline Offline
Newbie Poster

Recursive Backtracking Search Problem

 
0
  #1
Nov 13th, 2007
I have to use a recursive backtracking search to solve the classic "bucket" problem. ie. You have two buckets, one holds 5L, the other 3L. Find a way to get 4L into the 5L bucket, while only being able to fill/empty either bucket, or to pour one of them into the other one.

The solution to this problem is easy to find, the problem is writing the search. So far, my code seems to try various combinations of instructions, and then exits without finding or printing the solution.

It is in "debug mode", printing out each step and the status after completion, so you'll have to hit enter to step through the instructions.

Any help or advice would be greatly appreciated.

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. struct step
  6. {
  7. int b3;
  8. int b5;
  9. int stype;
  10. };
  11.  
  12. int fill_3(int *bucket)
  13. {
  14. *bucket=3;
  15. return 1;
  16. }
  17.  
  18. int fill_5(int *bucket)
  19. {
  20. *bucket=5;
  21. return 2;
  22. }
  23.  
  24. int empty_3(int *bucket)
  25. {
  26. *bucket=0;
  27. return 3;
  28. }
  29.  
  30. int empty_5(int *bucket)
  31. {
  32. *bucket=0;
  33. return 4;
  34. }
  35.  
  36. int pour_3_into_5(int *three, int *five)
  37. {
  38. int poured;
  39. int overflow;
  40.  
  41. poured = *three;
  42.  
  43. *three -= poured;
  44. *five += poured;
  45.  
  46. overflow=(*five-5);
  47.  
  48. if (overflow>0)
  49. {
  50. *five -= overflow;
  51. *three += overflow;
  52. }
  53. return 5;
  54. }
  55.  
  56. int pour_5_into_3(int *five, int *three)
  57. {
  58. int poured;
  59. int overflow;
  60.  
  61. poured = *five;
  62.  
  63. *five -= poured;
  64. *three += poured;
  65.  
  66. overflow=(*three-3);
  67.  
  68. if (overflow>0)
  69. {
  70. *three -= overflow;
  71. *five += overflow;
  72. }
  73. return 6;
  74. }
  75.  
  76. void print_state(int three, int five)
  77. {
  78. printf("-----------------------------\nBucket 3 holds: %dL\nBucket 5 holds: %dL\n",three,five);
  79. }
  80.  
  81. void print_step(int stype)
  82. {
  83. switch(stype)
  84. {
  85. case 1:
  86. printf("\nFill bucket 3\n");
  87. break;
  88. case 2:
  89. printf("\nFill bucket 5\n");
  90. break;
  91. case 3:
  92. printf("\nEmpty bucket 3\n");
  93. break;
  94. case 4:
  95. printf("\nEmpty bucket 5\n");
  96. break;
  97. case 5:
  98. printf("\nPour bucket 3 into bucket 5\n");
  99. break;
  100. case 6:
  101. printf("\nPour bucket 5 into bucket 3\n");
  102. break;
  103. }
  104. }
  105.  
  106. void print_solution(struct step *s, int *position)
  107. {
  108. int x;
  109.  
  110. printf("\nSOLUTION\n");
  111. printf("--------");
  112.  
  113. for (x=0; x<=*position; x++)
  114. {
  115. print_step(s[x].stype);
  116. }
  117. }
  118.  
  119. void find_solution(int *b3, int *b5, int *position, struct step *s)
  120. {
  121. int i,x;
  122. char *dummy;
  123.  
  124. int found=0;
  125.  
  126. int temp3,temp5;
  127.  
  128. temp3=*b3;
  129. temp5=*b5;
  130.  
  131. for (i=1; i<=6; i++)
  132. {
  133. switch(i)
  134. {
  135. case 1:
  136. temp3=*b3;
  137. temp5=*b5;
  138. fill_3(&temp3);
  139. if (temp5==4)
  140. {
  141. print_solution(s,position);
  142. print_step(i);
  143. printf("------------------\n");
  144. return;
  145. }
  146. print_step(i);
  147. print_state(temp3, temp5);
  148. scanf("%c",dummy);
  149.  
  150. found=0;
  151. for (x=0; x<*position; x++)
  152. {
  153. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  154. {
  155. found=1;
  156. continue;
  157. }
  158. }
  159.  
  160. if (!found)
  161. {
  162. s[*position].b3=temp3;
  163. s[*position].b5=temp5;
  164. (*position)++;
  165. *b3=temp3;
  166. *b5=temp5;
  167. find_solution(b3,b5,position,s);
  168. }else{
  169. break;
  170. }
  171. case 2:
  172. temp3=*b3;
  173. temp5=*b5;
  174. fill_5(&temp5);
  175. if (temp5==4)
  176. {
  177. print_solution(s, position);
  178. print_step(i);
  179. printf("------------------\n");
  180. return;
  181. }
  182. print_step(i);
  183. print_state(temp3, temp5);
  184. scanf("%c",dummy);
  185.  
  186. found=0;
  187. for (x=0; x<*position; x++)
  188. {
  189. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  190. {
  191. found=1;
  192. continue;
  193. }
  194. }
  195.  
  196. if (!found)
  197. {
  198. s[*position].b3=temp3;
  199. s[*position].b5=temp5;
  200. (*position)++;
  201. *b3=temp3;
  202. *b5=temp5;
  203. find_solution(b3,b5,position,s);
  204. }else{
  205. break;
  206. }
  207. case 3:
  208. temp3=*b3;
  209. temp5=*b5;
  210. empty_3(&temp3);
  211. if (temp5==4)
  212. {
  213. print_solution(s, position);
  214. print_step(i);
  215. printf("------------------\n");
  216. return;
  217. }
  218. print_step(i);
  219. print_state(temp3, temp5);
  220. scanf("%c",dummy);
  221.  
  222. found=0;
  223. for (x=0; x<*position; x++)
  224. {
  225. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  226. {
  227. found=1;
  228. continue;
  229. }
  230. }
  231.  
  232. if (!found)
  233. {
  234. s[*position].b3=temp3;
  235. s[*position].b5=temp5;
  236. (*position)++;
  237. *b3=temp3;
  238. *b5=temp5;
  239. find_solution(b3,b5,position,s);
  240. }else{
  241. break;
  242. }
  243. case 4:
  244. temp3=*b3;
  245. temp5=*b5;
  246. empty_5(&temp5);
  247. if (temp5==4)
  248. {
  249. print_solution(s, position);
  250. print_step(i);
  251. printf("------------------\n");
  252. return;
  253. }
  254. print_step(i);
  255. print_state(temp3, temp5);
  256. scanf("%c",dummy);
  257.  
  258. found=0;
  259. for (x=0; x<*position; x++)
  260. {
  261. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  262. {
  263. found=1;
  264. continue;
  265. }
  266. }
  267.  
  268. if (!found)
  269. {
  270. s[*position].b3=temp3;
  271. s[*position].b5=temp5;
  272. (*position)++;
  273. *b3=temp3;
  274. *b5=temp5;
  275. find_solution(b3,b5,position,s);
  276. }else{
  277. break;
  278. }
  279. case 5:
  280. temp3=*b3;
  281. temp5=*b5;
  282. pour_3_into_5(&temp3,&temp5);
  283. if (temp5==4)
  284. {
  285. print_solution(s, position);
  286. print_step(i);
  287. printf("------------------\n");
  288. return;
  289. }
  290. print_step(i);
  291. print_state(temp3, temp5);
  292. scanf("%c",dummy);
  293.  
  294. found=0;
  295. for (x=0; x<*position; x++)
  296. {
  297. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  298. {
  299. found=1;
  300. continue;
  301. }
  302. }
  303.  
  304. if (!found)
  305. {
  306. s[*position].b3=temp3;
  307. s[*position].b5=temp5;
  308. (*position)++;
  309. *b3=temp3;
  310. *b5=temp5;
  311. find_solution(b3,b5,position,s);
  312. }else{
  313. break;
  314. }
  315. case 6:
  316. temp3=*b3;
  317. temp5=*b5;
  318. pour_5_into_3(&temp5,&temp3);
  319. if (temp5==4)
  320. {
  321. print_solution(s, position);
  322. print_step(i);
  323. printf("------------------\n");
  324. return;
  325. }
  326. print_step(i);
  327. print_state(temp3, temp5);
  328. scanf("%c",dummy);
  329.  
  330. found=0;
  331. for (x=0; x<*position; x++)
  332. {
  333. if((s[x].b3==temp3)&&(s[x].b5==temp5))
  334. {
  335. found=1;
  336. continue;
  337. }
  338. }
  339.  
  340. if (!found)
  341. {
  342. s[*position].b3=temp3;
  343. s[*position].b5=temp5;
  344. (*position)++;
  345. *b3=temp3;
  346. *b5=temp5;
  347. find_solution(b3,b5,position,s);
  348. }else{
  349. break;
  350. }
  351. }
  352. }
  353.  
  354. }
  355.  
  356. int main()
  357. {
  358. int b3=0;
  359. int b5=0;
  360. /*int stemp;*/
  361.  
  362. struct step s[1000];
  363.  
  364. int position=0;
  365.  
  366. find_solution(&b3,&b5, &position, s);
  367.  
  368. return 0;
  369. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,042
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 178
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: Recursive Backtracking Search Problem

 
0
  #2
Nov 13th, 2007
Just something to think about it.

  1. /*
  2.  * bleh.c
  3.  */
  4. #include <stdio.h>
  5.  
  6. int main ( void )
  7. {
  8. int i;
  9.  
  10. for ( i = 1; i <= 6; i++ )
  11. {
  12. switch( i )
  13. {
  14. case 1:
  15. puts( "One here, and two's coming right up" );
  16. break;
  17. case 2:
  18. puts( "Two here, and three's coming very soon" );
  19. break;
  20. case 3:
  21. puts( "Three here, and four is at the corner" );
  22. break;
  23. case 4:
  24. puts( "Four reporting in, and I met five coming up stairs" );
  25. break;
  26. case 5:
  27. puts( "Five is present, and six is at the door" );
  28. break;
  29. case 6:
  30. puts( "Six ready, and default will never happen" );
  31. break;
  32. default:
  33. puts( "I work in secret ways" );
  34. break;
  35. }
  36. }
  37. getchar();
  38. return 0;
  39. }
Last edited by Aia; Nov 13th, 2007 at 6:57 pm.
"If it moves, tax it. If it keeps moving, regulate it, and if it stops moving, subsidize it" - Ronald Reagan
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Recursive Backtracking Search Problem

 
0
  #3
Nov 13th, 2007
> char *dummy;
Well you're spraying random data all over memory with your scanf calls, so the code is broken before it's begun.

Is it me, or is every single case of your find_solution() identical, except for one line?
Consider simplifying the code if this is the case.

You're also ignoring all the return results of all your 'fill' and 'empty' functions. If you really don't need them, then make them all void.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC