Guest User

Untitled

a guest
Jul 16th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.61 KB | None | 0 0
  1. movelist solve(board B)
  2. //@requires is_board(B);
  3. {
  4.     queue allQueue = queue_new();
  5.     int size = 1;
  6.     for (int i = 0; i < B->width*B->height; i++) {
  7.         size *= 2;
  8.     }
  9.     hmap movesSeen = hmap_new(size);
  10.     bool start = hmap_insert(B, NULL, movesSeen);
  11.     enqueue(B, allQueue);
  12.     board end = alloc(struct board);
  13.     end->width = B->width;
  14.     end->height = B->height;
  15.     end->lights = 0;
  16.  
  17.     if (hmap_ktype_equal(B, end)) return NULL;
  18.     while (!(queue_empty(allQueue))) {
  19.         board check = dequeue(allQueue);
  20.         if (hmap_ktype_equal(check, end)) {
  21.             board solBoard = copy_board(check);
  22.             move nextMove = hmap_get(solBoard, movesSeen);
  23.             movelist sol = movelist_new(nextMove->x, nextMove->y, NULL);
  24.             untoggle_board(solBoard, nextMove->x, nextMove->y);
  25.             while (!hmap_ktype_equal(solBoard, B)) {
  26.                 nextMove = hmap_get(solBoard, movesSeen);
  27.                 sol = movelist_new(nextMove->x, nextMove->y, sol);
  28.                 untoggle_board(solBoard, nextMove->x, nextMove->y);
  29.             }
  30.  
  31.             return sol;
  32.         }
  33.         for (int x = 0; x < (B->width); x++) {
  34.             for (int y = 0; y < (B->height); y++) {
  35.                 board newBoard = copy_board(check);
  36.                 toggle_board(newBoard, x, y);
  37.                 move newMove = move_new(x, y);
  38.                 if (!(hmap_contains_key(newBoard, movesSeen))) {
  39.                     hmap_insert(newBoard, newMove, movesSeen);
  40.                     enqueue(newBoard, allQueue);
  41.                 }
  42.             }
  43.         }
  44.     }
  45.     return NULL;
  46. }
Add Comment
Please, Sign In to add comment