Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions coding_freshmen/C++/Vikas-hub-cyber/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@


# Sliding-Puzzle-Game solver
### Implementation of BFS Algorithms to solve N*M sliding puzzle Game.

The project is based on the solution of [Sliding puzzle Game](https://en.wikipedia.org/wiki/Sliding_puzzle) in which Slide your tiles around the game board until you have arranged them in the correct order
<p align="center">
<img width="460" height="460" src="https://miro.medium.com/max/1213/1*YxeZJzfhW4kn5O5wAGbkIg.gif">
</p>
<br />

- Concept of [Breadth-first search(BFS)](https://en.wikipedia.org/wiki/Breadth-first_search) to consider all cases
- Created own singly linked list
- linked list based queue for BFS .
- Convert grid into string of digits so that easy to process
- Convert string into grid finally,
42 changes: 42 additions & 0 deletions coding_freshmen/C++/Vikas-hub-cyber/mylinkedlist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

//** singly Lindked list operation operation *//

#include <bits/stdc++.h>

//node of singly linked list
struct listNode
{
struct listNode *next; //next node linked to current node
char *data; //string state contained by the list
};

struct listNode *head = NULL;

//Returns 1 if value is found in the list, else 0
//Time complexity : O(N)
int isPresent(char *value)
{
struct listNode *dummy = head;
while (dummy)
{
//if data of dummy node and value is same
if (strcmp(value, dummy->data) == 0)
return 1;

dummy = dummy->next;
}
return 0;
}

//Inserts a new value in the list
//Time complexity : O(1)
void insert(char *value)
{
struct listNode *newNode = (struct listNode *)malloc(sizeof(struct listNode));

newNode->data = (char *)malloc(sizeof(char) * 10);
strcpy(newNode->data, value);
newNode->next = head;

head = newNode;
}
111 changes: 111 additions & 0 deletions coding_freshmen/C++/Vikas-hub-cyber/myqueue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**** Queue using linked list***/

#include <bits/stdc++.h>
#include <assert.h>

// node of tree-linked list
struct Node
{
Node *next; // pointer to next node of the list
Node *parent; // parent node of this node to be pushed to queue
char *val; // value contained by the node
int valMoved; // value moved to occupy the empty space
};

typedef struct Node queueNode;

typedef struct
{
queueNode *front; // front of the queue
queueNode *rear; // rear of the queue
int size;
} Queue;

Queue *q;

// returns 1 if queue is empty else 0
int is_empty()
{
return q->front == NULL;
}

// pushes an elment at back of the queue
void enqueue(char *value, int valueMoved, queueNode *parentNode)
{
q->size++;
queueNode *dummy = new queueNode();

dummy->val = (char *)malloc(sizeof(char) * 10);
strcpy(dummy->val, value);

dummy->next = NULL;
dummy->parent = parentNode;
dummy->valMoved = valueMoved;

if (is_empty())
q->rear = q->front = dummy;
else
{
q->rear->next = dummy;
q->rear = q->rear->next;
}
}

// removes the front element from the queue
void dequeue()
{
// queue should not be empty
assert(!is_empty());
q->size--;

q->front = q->front->next;
}

// returns the element at front of the queue
char *frontVal()
{
// queue should not be empty
assert(!is_empty());

return q->front->val;
}

// returns the node at front of the queue
queueNode *frontNode()
{
// queue should not be empty
assert(!is_empty());

return q->front;
}

// returns the size of the queue
int queueSize()
{
return q->size;
}

// function to initialize the queue
void initializeQueue()
{
q = (Queue *)malloc(sizeof(Queue));
q->front = NULL;
q->rear = NULL;
q->size = 0;
}

// reverses a path of parent nodes
queueNode *reversePath(queueNode *head)
{
queueNode *previousNode = NULL;

while (head)
{
queueNode *Node = head->parent;
head->parent = previousNode;
previousNode = head;
head = Node;
}

return previousNode;
}
Loading