Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int manhattanCost(Board b, Board goal) {
- int cost = 0;
- int count = 1;
- int max = b->size * b->size;
- while (count < max) {
- int posCurrent = getPosition(b, count);
- int posGoal = getPosition(goal, count);
- if (posCurrent != posGoal) {
- if (b->size == 4) {
- cost += abs(posCurrent/b->size - posGoal/b->size) +
- abs(posCurrent%b->size - posGoal%b->size);
- } else {
- cost += abs(posCurrent - posGoal)/b->size +
- abs(posCurrent - posGoal)%b->size;
- }
- }
- count++;
- }
- return cost;
- }
- int outOfPlaceCost(Board b, Board goal) {
- int cost = 0;
- int count = 1;
- int max = b->size * b->size;
- while (count < max) {
- int posCurrent = getPosition(b, count);
- int posGoal = getPosition(goal, count);
- if (posCurrent != posGoal) {
- cost++;
- }
- count++;
- }
- return cost;
- }
Add Comment
Please, Sign In to add comment