A sliding puzzle is a puzzle that challenges a player to slide flat pieces along certain routes on a k × k board to establish a certain end-configuration. The fifteen puzzle is the oldest type of sliding block puzzle and it was invented by Noyes Chapman in 1880s. It is played on a 4-by-4 grid with 15 square blocks labeled 1 through 15 and a blank square. The goal is to rearrange the blocks so that they are in order, using as few moves as possible. Blocks are permitted to be slid horizontally or vertically into the blank square. Search time for the goal state can be shortened by adding a priority for the direction of the search to proceed. A* algorithm is commonly used as an efficient alternative to find an answer to the sliding puzzle with the minimum number of steps.  you will implement A* algorithm to solve the sliding puzzle problem.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

*Data Structures and Algorithm

Sliding (or k 2 − 1) puzzle using A* algorithm

A sliding puzzle is a puzzle that challenges a player to slide flat pieces along certain routes on a k × k board to establish a certain end-configuration. The fifteen puzzle is the oldest type of sliding block puzzle and it was invented by Noyes Chapman in 1880s. It is played on a 4-by-4 grid with 15 square blocks labeled 1 through 15 and a blank square. The goal is to rearrange the blocks so that they are in order, using as few moves as possible. Blocks are permitted to be slid horizontally or vertically into the blank square.

Search time for the goal state can be shortened by adding a priority for the direction of the search to proceed. A* algorithm is commonly used as an efficient alternative to find an answer to the sliding puzzle with the minimum number of steps.  you will implement A* algorithm to solve the sliding puzzle problem.

 

A* Algorithm 

A* (pronounced A-star) is an informed search algorithm, or a best-first search, formulated in terms of weighted graphs starting from a specific starting node of a graph, and find a path to the given goal node having the smallest cost (least distance, shortest time, etc.). A* maintains a tree of paths originating at the start node and extends those paths one edge at a time until its termination criterion is satisfied. At each iteration of its main loop, A* needs to determine which of its paths to extend based on: i) the cost of the path taken so far and ii)an estimate of the cost required to extend the path towards the goal Thus, A* selects the path that minimizes f(n) = g(n) + h(n), where • n is the next node on the path, • g(n) is the cost of the path from the start node to n, and • h(n) is a heuristic function that estimates the cost of the cheapest path from n to the goal.

Files to work with:

starter.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char **argv)
{
FILE *fp_in,*fp_out;

fp_in = fopen(argv[1], "r");
if (fp_in == NULL){
 printf("Could not open a file.\n");
 return -1;
}

fp_out = fopen(argv[2], "w");
if (fp_out == NULL){
 printf("Could not open a file.\n");
 return -1;
}

char *line = NULL;
size_t lineBuffSize = 0;
ssize_t lineSize;
int k;

getline(&line,&lineBuffSize,fp_in);//ignore the first line in file, which is a comment
fscanf(fp_in,"%d\n",&k);//read size of the board
//printf("k = %d\n", k); //make sure you read k properly for DEBUG purposes
getline(&line,&lineBuffSize,fp_in);//ignore the second line in file, which is a comment

int initial_board[k*k];//get kxk memory to hold the initial board
for(int i=0;i<k*k;i++)
 fscanf(fp_in,"%d ",&initial_board[i]);
//printBoard(initial_board, k);//Assuming that I have a function to print the board, print it here to make sure I read the input board properly for DEBUG purposes
fclose(fp_in);

////////////////////
// do the rest to solve the puzzle
////////////////////

//once you are done, you can use the code similar to the one below to print the output into file
//if the puzzle is NOT solvable use something as follows
fprintf(fp_out, "#moves\n");
fprintf(fp_out, "no solution\n");

//if it is solvable, then use something as follows:
fprintf(fp_out, "#moves\n");
//probably within a loop, or however you stored proper moves, print them one by one by leaving a space between moves, as below
for(int i=0;i<numberOfMoves;i++)
 fprintf(fp_out, "%d ", move[i]);

fclose(fp_out);

return 0;

}
 

 

makefile

SOURCE := main.c
CC := gcc
TARGET := solve

all: $(TARGET)

$(TARGET): $(SOURCE)
$(CC) -o $(TARGET) $(SOURCE)

clean:
rm -f $(TARGET)

 

3boardEasy.txt

#k
3
#initial state
1 0 3 4 2 6 7 5 8 

 

3boardEasyOut.txt

moves
2 5 8 

 

3boardHard.txt

#k
3
#initial state
1 2 8 4 5 0 7 6 3 
 

3boardHardOut.txt

#moves
8 2 5 8 3 6 8 5 2 3 6 

 

#k
4
#initial state
1 2 3 4 5 6 7 8 9 10 15 11 13 14 0 12
first and third line will be there for comment. You will ignore them while reading the file.
second line will denote the number of rows (or columns) of board. i.e.,if k = 4, that means you will
read 16 labels from the input file. Your program can be tested for values of k where k < 10. We're
not going to test your program for crazy cases. Time is valuable for all of us :)
fourth line consists of the labels of the puzzle in reading order from left to right, top to bottom.
For instance, input example given above is the reading order of the board that was given as the example
in previous page.
empty block is going to be denoted by 0. The rest of the labels are going to be the integers from 1 to
k2 – 1.
• Execution of the program: Your program is going to calculate the moves necessary to make in order to
arrive to the goal state. As stated earlier in the previous page, goal state is the state where labels are in the
ollowing order over the example board of 4x4:
1 2 3 4 5 6 7 89 10 11 12 13 14 15 0
That is, all the labels are in sorted order from left to right, top to bottom, and the empty label (denoted by 0)
is at the bottom right corner of the board.
A solution is going to be considered invalid if either one of the following occurs:
- Invalid move: In a board, you can only move the tiles that are around the empty block, and you can
only move them orthogonally. Thus, diagonal moves, or moves that take a tile which is not in immediate
vicinity of empty block is considered invalid.
Repeated state: If the board state that is to be produced after a move is identical to a state which was
previously created by your program, this will be considered as an invalid move.. (Our evaluation program
will keep track of intermediate states, and will check if you are revisiting an already visited board state)
• Output file needs to strictly satisfy the following structure:
#тoves
15 11 12
- first line is comment to be ignored.
second line consists of labels of the blocks to be moved at a time, first move being the leftmost, and
each move separated by a white space. For instance, the example output given above is a valid output
to solve the initial state given in first page of this document. It stands for moving the blocks 15, 11, and
finally 12 to arrive to the goal state.
- If the test case that is supplied does not have a solution, you should write "no solution" to the output file
in the second line. It is important to note that, half of the possible inputs in a k2 – 1 puzzle does not
have a solution. You need to find put a way to determine whether a solution exists or not (google will
definitely help on that).
• Report file should provide the following information:
Explain the data structure that you used. How did you implement it?
What is the space (memory) complexity of your algorithm? Could it be better? If so, what would be the
trade off?
- Explain the algorithm that you used. What is its running time? Could there be a better algorithm? Does
it give the optimal solution? If it does not, would there be an algorithm that gives optimal solution?
How did you calculate whether a valid solution existed for the problem or not?
Transcribed Image Text:#k 4 #initial state 1 2 3 4 5 6 7 8 9 10 15 11 13 14 0 12 first and third line will be there for comment. You will ignore them while reading the file. second line will denote the number of rows (or columns) of board. i.e.,if k = 4, that means you will read 16 labels from the input file. Your program can be tested for values of k where k < 10. We're not going to test your program for crazy cases. Time is valuable for all of us :) fourth line consists of the labels of the puzzle in reading order from left to right, top to bottom. For instance, input example given above is the reading order of the board that was given as the example in previous page. empty block is going to be denoted by 0. The rest of the labels are going to be the integers from 1 to k2 – 1. • Execution of the program: Your program is going to calculate the moves necessary to make in order to arrive to the goal state. As stated earlier in the previous page, goal state is the state where labels are in the ollowing order over the example board of 4x4: 1 2 3 4 5 6 7 89 10 11 12 13 14 15 0 That is, all the labels are in sorted order from left to right, top to bottom, and the empty label (denoted by 0) is at the bottom right corner of the board. A solution is going to be considered invalid if either one of the following occurs: - Invalid move: In a board, you can only move the tiles that are around the empty block, and you can only move them orthogonally. Thus, diagonal moves, or moves that take a tile which is not in immediate vicinity of empty block is considered invalid. Repeated state: If the board state that is to be produced after a move is identical to a state which was previously created by your program, this will be considered as an invalid move.. (Our evaluation program will keep track of intermediate states, and will check if you are revisiting an already visited board state) • Output file needs to strictly satisfy the following structure: #тoves 15 11 12 - first line is comment to be ignored. second line consists of labels of the blocks to be moved at a time, first move being the leftmost, and each move separated by a white space. For instance, the example output given above is a valid output to solve the initial state given in first page of this document. It stands for moving the blocks 15, 11, and finally 12 to arrive to the goal state. - If the test case that is supplied does not have a solution, you should write "no solution" to the output file in the second line. It is important to note that, half of the possible inputs in a k2 – 1 puzzle does not have a solution. You need to find put a way to determine whether a solution exists or not (google will definitely help on that). • Report file should provide the following information: Explain the data structure that you used. How did you implement it? What is the space (memory) complexity of your algorithm? Could it be better? If so, what would be the trade off? - Explain the algorithm that you used. What is its running time? Could there be a better algorithm? Does it give the optimal solution? If it does not, would there be an algorithm that gives optimal solution? How did you calculate whether a valid solution existed for the problem or not?
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY