Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a 2D array that a user can input up, down, left, and right to try to solv

ID: 3790286 • Letter: I

Question

I have a 2D array that a user can input up, down, left, and right to try to solve a sliding 8 puzzle. I need to use breadth first search now so that the computer can solve it. I would like to use a queue and store the array coordinates of the move such as A[1][1] was U3 so that the whole thing can be solved with BFS. Code in C or C++. Heres a sample if user entered right.

case 'r':

if(row < 3 && row > -1)

{

cmove = col + 1;

rmove = row;

if(cmove < 3 && cmove >= 0)

{

i = A[rmove][cmove];

A[rmove][cmove] = 0;

A[row][col] = i;

}

   }

break;

Explanation / Answer

// function to print the top-right peel of the matrix and
// recursively call the print bottom-left on the submatrix.
void printTopRight(int a[][COL], int x1, int y1, int x2, int y2) {
int i = 0, j = 0;

// print values in the row.
for(i = x1; i<=x2; i++) {
printf("%d ", a[y1][i]);
}

// print values in the column.
for(j = y1 + 1; j <= y2; j++) {
printf("%d ", a[j][x2]);
}

// see if more layers need to be printed.
if(x2-x1 > 0) {
// if yes recursively call the function to
// print the bottom left of the sub matrix.
printBottomLeft(a, x1, y1 + 1, x2-1, y2);
}
}

// function to print the bottom-left peel of the matrix and
// recursively call the print top-right on the submatrix.
void printBottomLeft(int a[][COL], int x1, int y1, int x2, int y2) {
int i = 0, j = 0;

// print the values in the row in reverse order.
for(i = x2; i>=x1; i--) {
printf("%d ", a[y2][i]);
}

// print the values in the col in reverse order.
for(j = y2 - 1; j >= y1; j--) {
printf("%d ", a[j][x1]);
}

// see if more layers need to be printed.
if(x2-x1 > 0) {
// if yes recursively call the function to
// print the top right of the sub matrix.
printTopRight(a, x1+1, y1, x2, y2-1);
}
}

void printSpiral(int arr[][COL]) {
printTopRight(arr,0,0,COL-1,ROW-1);
printf(" ");
}