
*Data Structures And
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



Trending nowThis is a popular solution!
Step by stepSolved in 3 steps

- C++ program Without using vectorarrow_forwardIn c++arrow_forward] ] has_perfect You are to write a function has "perfect (graph)" that takes in a BIPARTITE graph as its input, and then determintes whether or not the graph has a perfect matching. In other words, it will return the Boolean value True if it has one, and False if it does not. After compiling the above cell, you should be able to compile the following cell and obtain the desired outputs. print (has perfect({"A" : ["B", "C"], "B" : ["A", "D"], "C" : ["A", "D"], "D" : ["B", "C"]}), has perfect ( {"A" : ["B"], "B" : ["A", "D", "E"], "C" : ["E"], "D":["B"], "E": ["B","C","F"], "F":["E"]})) This should return True False Python Pythonarrow_forward
- a) Write a program that asks user to enter number of vertices in a directed graph and then the adjacency matrix representing the directed graph. The program, then, must display the node with the highest outdegree. Assume that nodes are named as 0, 1, 2 and so on. You will find a sample run of the program below. Enter number of vertices: 4 Enter adjacency matrix: 0 1 1 1 0 0 0 0 0 0 0 0 0 1 1 0 Node with the highest outdegree: 0arrow_forwardC++ The binary search algorithm given in this chapter is nonrecursive. Write and implement a recursive version of the binary search algorithm. The program should prompt Y / y to continue searching and N / n to discontinue. If an item is found in the list display the following message: x found at position y else: x is not in the list Use the following list of integers when testing your program: 2, 6, 8, 13, 25, 33, 39, 42, 53, 58, 61, 68, 71, 84, 97arrow_forward] ] is_bipartite In the cell below, you are to write a function "is_bipartite (graph)" that takes in a graph as its input, and then determines whether or not the graph is bipartite. In other words, it returns True if it is, and False if it is not. After compiling the above cell, you should be able to compile the following cell and obtain the desired outputs. print (is_bipartite({"A" : ["B", "C"], "B" : ["A"], "C" : ["A"]}), is_bipartite({"A" : ["B", "C"], "B" : ["A", "C"], "C" : ["A", "B"]})) This should return True False Python Pythonarrow_forward
- Python question Analysis: Invariants (Q16-17) For each of the following functions, identify the loop invariant, exit condition and post condition. Question 16 (Invariant 1) This function returns the list of integers that are multiples of both 3 and 7 that occurs within a given list. Identify the loop exit condition, the loop post-condition, and the loop invariant at the end of the loop, which shows the algorithm’s correctness. def multiples_count(lst): """ Input: a list of integers, lst Output: the list of integers in lst that are multiples of both 3 and 7 """ res = [] for i in range(len(lst)): if lst[i]%3 == 0 and lst[i]%7 == 0: res.append(lst[i]) # Identify the loop invariant here return res Question 17 (Invariant 2) This function checks if a given list (of comparable elements) is sorted in ascending order. Identify the loop exit condition, the loop post-condition, and the loop invariant at the end of each iteration of the loop, which…arrow_forwardPython Big-O Notation/Time Complexity Pls answer only if u know big-o Thanks! Item #3.arrow_forward] ] get_nhbr In the cell below, you are to write a function called "get_nhbr(Ist, graph)" that takes in two inputs: a list of vertices and a graph. The function is to return a list that contains the neighborhood of the vertices in 'Ist' (remember that this means you are finding the union of the individual vertices' neighborhoods). + Code + Markdown After compiling the above cell, you should be able to compile the following cell and obtain the desired outputs. print (get_nhbr(["A", "D"], {"A" : ["B"], "B" : ["A", "D", "E"], "C" : ["E"], "D":["B"], "E":["B","C","F"], "F":["E"]}), get_nhbr(["B", "C", "F"], {"A" : ["B"], "B" : ["A", "D", "E"], "C" : ["E"], "D":["B"], "E":["B","C","F"], "F":["E"]})) This should return ["B"] ["A", "D", "E"] Python Pythonarrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





