Advertisement
ksong

Untitled

Nov 26th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.29 KB | None | 0 0
  1.  
  2. #include <stdlib.h>
  3. #include <stdint.h>
  4. #include <stdbool.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include "lib/contracts.h"
  9. #include "lib/bitvector.h"
  10. #include "lib/queue.h"
  11. #include "board-ht.h"
  12. #include "lib/hset.h"
  13. #include "lib/boardutil.h"
  14. #include "lib/xalloc.h"
  15.  
  16. //counts number of lights on in a given board
  17. int num_light(bitvector board)
  18. {
  19.   int count=0;
  20.   for (int i=0; i<BITVECTOR_LIMIT; i++){
  21.     if(bitvector_get(board, i)){
  22.       count++;
  23.     }
  24.   }
  25.   return count;
  26. }
  27. /*
  28. void press_button(bitvector *newboard, int row, int col, uint8_t width,
  29.                         uint8_t height, struct board_data *B)
  30. {
  31.   //left
  32.   if (is_valid_pos(row, col-1, width, height)){
  33.     uint8_t il = get_index(row, col-1, width, height);
  34.     *newboard = bitvector_flip(*newboard, il);
  35.   }
  36.   //right
  37.   if (is_valid_pos(row, col+1, width, height)){
  38.     uint8_t ir = get_index(row, col+1, width, height);
  39.     *newboard = bitvector_flip(*newboard, ir);
  40.   }
  41.   //up
  42.   if(is_valid_pos(row-1, col, width, height)){
  43.     uint8_t iu = get_index(row-1, col, width, height);
  44.     *newboard = bitvector_flip(*newboard, iu);
  45.   }
  46.   //down
  47.   if(is_valid_pos(row+1, col, width, height)){
  48.     uint8_t id = get_index(row+1, col, width, height);
  49.     *newboard = bitvector_flip(*newboard,id);
  50.   }
  51.   //keep track of push moves
  52.   B->pushes = bitvector_flip(B->pushes, get_index(row, col, width, height));
  53. }
  54. */
  55. int main(int argc, char **argv) {
  56.   if (argc != 2) {
  57.     fprintf(stderr, "Usage: lightsout <board name>\n");
  58.     return 1;
  59.   }
  60.   char *board_filename = argv[1];
  61.  
  62.   queue_t Q = queue_new();
  63.   hset_t H = ht_new(1000);
  64.   bitvector new = bitvector_new();
  65.   uint8_t width = 0;
  66.   uint8_t height = 0;
  67.   //unsuccessful file read
  68.   if (file_read(board_filename, &new, &width, &height)==false){
  69.     fprintf(stderr, "unsuccessful file read\n");
  70.     return 1;
  71.   }
  72.  
  73.   struct board_data *B = xmalloc(sizeof(struct board_data));
  74.   B->board = new;
  75.   B->pushes = bitvector_new();
  76.   enq(Q, B);
  77.   ht_insert(H, B);
  78.  
  79.   while (!queue_empty(Q)) {
  80.   // Find a board that we haven’t looked at yet from the queue
  81.     B = (struct board_data*)deq(Q);
  82.     // Consider all the moves
  83.     for (int row = 0; row < height; row++) {
  84.       for (int col = 0; col < width; col++) {
  85.         bitvector newboard = B->board;
  86.         if (is_valid_pos(row, col-1, width, height)){
  87.             uint8_t il = get_index(row, col-1, width, height);
  88.             newboard = bitvector_flip(newboard, il);
  89.           }
  90.           //right
  91.           if (is_valid_pos(row, col+1, width, height)){
  92.             uint8_t ir = get_index(row, col+1, width, height);
  93.             newboard = bitvector_flip(newboard, ir);
  94.           }
  95.           //up
  96.           if(is_valid_pos(row-1, col, width, height)){
  97.             uint8_t iu = get_index(row-1, col, width, height);
  98.             newboard = bitvector_flip(newboard, iu);
  99.           }
  100.           //down
  101.           if(is_valid_pos(row+1, col, width, height)){
  102.             uint8_t id = get_index(row+1, col, width, height);
  103.             newboard = bitvector_flip(newboard,id);
  104.           }
  105.           //keep track of push moves
  106.           B->pushes = bitvector_flip(B->pushes, get_index(row, col, width, height));
  107.         if (num_light(newboard) == 0) {
  108.           for (int i=0; i<(int)width*height; i++){
  109.             if (bitvector_get(B->pushes, i))
  110.             {
  111.               int a = i/width;
  112.               int b = i%width;
  113.               printf("%d:%d", a, b);
  114.             }
  115.           }
  116.           fprintf(stderr, "board could be solved bruh");
  117.           hset_free(H);
  118.           queue_free(Q, NULL); //is this how you free all memory?
  119.           return 0;
  120.         }
  121.         if (ht_lookup(H, B->board)==NULL) {
  122.         //hash table H doesn’t contain newboard
  123.           struct board_data *N = xmalloc(sizeof(struct board_data));
  124.           N->board = newboard;
  125.           //SET OTHER FIELDS
  126.           ht_insert(H, N);
  127.           enq(Q, (void *)N);
  128.           // Allocate memory for hashtable element N
  129.           // Set the field N->board to newboard, set other fields
  130.           // Insert N into the hashtable H
  131.           // Enqueue N into the queue Q
  132.         }
  133.       }
  134.     }
  135.   }
  136.   hset_free(H);
  137.   fprintf(stderr, "board cannot be solved\n");
  138.   queue_free(Q, NULL);  return 1;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement