使用BFS改进8-Puzzle

时间:2015-10-07 04:04:13

标签: c algorithm queue breadth-first-search 8-puzzle

我试图在我尝试解决8益智游戏时实现广度优先搜索算法。但在某些情况下,我的内存不足,但在更简单的情况下,它可以毫无问题地解决。

如何改进算法来修复它?

的main.c

/* Standard Libraries */
#include <stdio.h>
#include <stdlib.h>

/* Game */
#include <8puzzle/queue.h>
#include <8puzzle/node.h>
#include <8puzzle/state.h>
#include <8puzzle/action.h>
#include <8puzzle/game.h>

/* Main */
int main(void)
{
  /* Queue containing the States */
  Queue *q = create_queue();

  /* Create Root State */
  State *root_state = malloc(sizeof(State));

  /* Read the State */
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
    {
      unsigned int temp;
      scanf("%u", &temp);
      root_state->game[i][j] = temp;
      if (temp == 0)
      {
        root_state->empty_space.line = i;
        root_state->empty_space.column = j;
      }
    }

  /* Create a Node */
  Node *root = malloc(sizeof(Node));
  root->data = root_state;

  /* Check if it's finished */
  if (is_finished(root->data))
  {
    printf("Já está finalizado.\n");
    return 0;
  }

  /* Add State to Queue */
  enqueue(q, root);

  /* Iterate while queue isn't empty */
  while (!is_queue_empty(q))
  {
    /* Get current node */
    Node *node = dequeue(q);

    /* Check if it's correct */
    if (is_finished(node->data))
    {
      Node *parent = node->prev;
      while (parent)
      {
        printf("1\n");
        parent = parent->prev;
      }
      return 0;
    }

    /* Generate possible moves */
    Coordinate *sucessors = malloc(4 * sizeof(Coordinate));
    int amount_of_sucessors = generate_state_sucessors(node->data, sucessors);

    /* For each new possibility of empty space coordinate */
    for (int i = 0; i < amount_of_sucessors; i++)
    {
      /* Create the new state */
      State *new_state = swap_state((State *)node->data, sucessors[i]);

      /* Add it to queue */
      Node *new_node = malloc(sizeof(Node));
      new_node->data = new_state;
      node->next = new_node;
      new_node->prev = node;
      enqueue(q, new_node);
    }
  }

  /* Return to operating system */
  return 0;
}

game.c

/**
 * Game Implementation
 *
 * This file will produce the implementation of the game, along with the BFS
 * algorithm to solve the 8-Puzzle Problem.
 */

/* Standard Libraries */
#include <stdbool.h>

/* Game Files */
#include <8puzzle/state.h>
#include <8puzzle/node.h>
#include <8puzzle/queue.h>
#include <8puzzle/action.h>
#include <8puzzle/game.h>

bool is_finished(State *s)
{
  if (s->game[0][0] != 1) return false;
  if (s->game[0][1] != 2) return false;
  if (s->game[0][2] != 3) return false;
  if (s->game[1][0] != 8) return false;
  if (s->game[1][2] != 4) return false;
  if (s->game[2][0] != 7) return false;
  if (s->game[2][1] != 6) return false;
  if (s->game[2][2] != 5) return false;
  return true;
}

1 个答案:

答案 0 :(得分:4)

8拼图的可能位置数是9! =(362880.)(从指定的起始位置无法到达其中一半,因此实际上只有181440种可能性。)

另一方面,解决这个难题可能需要多达31个动作according to wikipedia。在每个位置,有2,3或4个可能的移动。因此,广度优先搜索可以轻松地将2 ^ 31个位置排队。

这导致了明显的矛盾。当只有181440个位置可能时,BFS如何将2 ^ 31个位置排队?很简单,有很多不同的方法可以达到相同的电路板位置,BFS会将大量的重复列入。

解决方案:只排队尚未尝试的位置。这可以使用362880个布尔数组来完成,这些布尔值可以跟踪已经尝试过的位置。通过避免重复,您可以保证队列中的条目数永远不会超过181440.