Guest User

Untitled

a guest
Feb 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. int manhattanCost(Board b, Board goal) {
  2.  
  3. int cost = 0;
  4. int count = 1;
  5. int max = b->size * b->size;
  6.  
  7. while (count < max) {
  8.  
  9. int posCurrent = getPosition(b, count);
  10. int posGoal = getPosition(goal, count);
  11.  
  12. if (posCurrent != posGoal) {
  13.  
  14. if (b->size == 4) {
  15.  
  16. cost += abs(posCurrent/b->size - posGoal/b->size) +
  17. abs(posCurrent%b->size - posGoal%b->size);
  18.  
  19. } else {
  20.  
  21. cost += abs(posCurrent - posGoal)/b->size +
  22. abs(posCurrent - posGoal)%b->size;
  23.  
  24. }
  25.  
  26. }
  27.  
  28. count++;
  29.  
  30. }
  31.  
  32. return cost;
  33.  
  34. }
  35.  
  36.  
  37.  
  38. int outOfPlaceCost(Board b, Board goal) {
  39.  
  40. int cost = 0;
  41. int count = 1;
  42. int max = b->size * b->size;
  43.  
  44. while (count < max) {
  45.  
  46. int posCurrent = getPosition(b, count);
  47. int posGoal = getPosition(goal, count);
  48.  
  49. if (posCurrent != posGoal) {
  50.  
  51. cost++;
  52.  
  53. }
  54.  
  55. count++;
  56.  
  57. }
  58.  
  59. return cost;
  60.  
  61. }
Add Comment
Please, Sign In to add comment