*Data Structures And Algorithm (C Programming) You are going to make C program to solve the general k2 − 1 puzzle. Your program will read an initial state for a k × k table, calculate (preferably minimum number of) steps taking player from initial state to the goal state, and print the solution into an output file.This is a graph search problem and can be solved using several different methods. You will implement Breadth First Search (BFS).

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 (C Programming)

You are going to make C program to solve the general k2 − 1 puzzle. Your program will read an initial state for a k × k table, calculate (preferably minimum number of) steps taking player from initial state to the goal state, and print the solution into an output file.This is a graph search problem and can be solved using several different methods. You will implement Breadth First Search (BFS).

 

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?
5 INPUT/OUTPUT SPECIFICATIONS
• Your makefile needs to produce an executable named "solve", once we type "make" in command line.
• Your executable "solve" is going to take two command line arguments, first being the name of the input
file and the second being the name of the output file. Thus, an example call to your program will be as follows:
·/solve input.txt output.txt
• Input file is going to have the structure explained below. You do not need to make any error check for the
correctness of the input file. You can assume that input specifications are always going to be satisfied by the
test inputs.
Transcribed Image Text:5 INPUT/OUTPUT SPECIFICATIONS • Your makefile needs to produce an executable named "solve", once we type "make" in command line. • Your executable "solve" is going to take two command line arguments, first being the name of the input file and the second being the name of the output file. Thus, an example call to your program will be as follows: ·/solve input.txt output.txt • Input file is going to have the structure explained below. You do not need to make any error check for the correctness of the input file. You can assume that input specifications are always going to be satisfied by the test inputs.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

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