Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <stdio.h>
- #include <string.h>
- #include <assert.h>
- #include "lib/contracts.h"
- #include "lib/bitvector.h"
- #include "lib/queue.h"
- #include "board-ht.h"
- #include "lib/hset.h"
- #include "lib/boardutil.h"
- #include "lib/xalloc.h"
- //counts number of lights on in a given board
- int num_light(bitvector board)
- {
- int count=0;
- for (int i=0; i<BITVECTOR_LIMIT; i++){
- if(bitvector_get(board, i)){
- count++;
- }
- }
- return count;
- }
- /*
- void press_button(bitvector *newboard, int row, int col, uint8_t width,
- uint8_t height, struct board_data *B)
- {
- //left
- if (is_valid_pos(row, col-1, width, height)){
- uint8_t il = get_index(row, col-1, width, height);
- *newboard = bitvector_flip(*newboard, il);
- }
- //right
- if (is_valid_pos(row, col+1, width, height)){
- uint8_t ir = get_index(row, col+1, width, height);
- *newboard = bitvector_flip(*newboard, ir);
- }
- //up
- if(is_valid_pos(row-1, col, width, height)){
- uint8_t iu = get_index(row-1, col, width, height);
- *newboard = bitvector_flip(*newboard, iu);
- }
- //down
- if(is_valid_pos(row+1, col, width, height)){
- uint8_t id = get_index(row+1, col, width, height);
- *newboard = bitvector_flip(*newboard,id);
- }
- //keep track of push moves
- B->pushes = bitvector_flip(B->pushes, get_index(row, col, width, height));
- }
- */
- int main(int argc, char **argv) {
- if (argc != 2) {
- fprintf(stderr, "Usage: lightsout <board name>\n");
- return 1;
- }
- char *board_filename = argv[1];
- queue_t Q = queue_new();
- hset_t H = ht_new(1000);
- bitvector new = bitvector_new();
- uint8_t width = 0;
- uint8_t height = 0;
- //unsuccessful file read
- if (file_read(board_filename, &new, &width, &height)==false){
- fprintf(stderr, "unsuccessful file read\n");
- return 1;
- }
- struct board_data *B = xmalloc(sizeof(struct board_data));
- B->board = new;
- B->pushes = bitvector_new();
- enq(Q, B);
- ht_insert(H, B);
- while (!queue_empty(Q)) {
- // Find a board that we haven’t looked at yet from the queue
- B = (struct board_data*)deq(Q);
- // Consider all the moves
- for (int row = 0; row < height; row++) {
- for (int col = 0; col < width; col++) {
- bitvector newboard = B->board;
- if (is_valid_pos(row, col-1, width, height)){
- uint8_t il = get_index(row, col-1, width, height);
- newboard = bitvector_flip(newboard, il);
- }
- //right
- if (is_valid_pos(row, col+1, width, height)){
- uint8_t ir = get_index(row, col+1, width, height);
- newboard = bitvector_flip(newboard, ir);
- }
- //up
- if(is_valid_pos(row-1, col, width, height)){
- uint8_t iu = get_index(row-1, col, width, height);
- newboard = bitvector_flip(newboard, iu);
- }
- //down
- if(is_valid_pos(row+1, col, width, height)){
- uint8_t id = get_index(row+1, col, width, height);
- newboard = bitvector_flip(newboard,id);
- }
- //keep track of push moves
- B->pushes = bitvector_flip(B->pushes, get_index(row, col, width, height));
- if (num_light(newboard) == 0) {
- for (int i=0; i<(int)width*height; i++){
- if (bitvector_get(B->pushes, i))
- {
- int a = i/width;
- int b = i%width;
- printf("%d:%d", a, b);
- }
- }
- fprintf(stderr, "board could be solved bruh");
- hset_free(H);
- queue_free(Q, NULL); //is this how you free all memory?
- return 0;
- }
- if (ht_lookup(H, B->board)==NULL) {
- //hash table H doesn’t contain newboard
- struct board_data *N = xmalloc(sizeof(struct board_data));
- N->board = newboard;
- //SET OTHER FIELDS
- ht_insert(H, N);
- enq(Q, (void *)N);
- // Allocate memory for hashtable element N
- // Set the field N->board to newboard, set other fields
- // Insert N into the hashtable H
- // Enqueue N into the queue Q
- }
- }
- }
- }
- hset_free(H);
- fprintf(stderr, "board cannot be solved\n");
- queue_free(Q, NULL); return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement