Can someone help me to improve the following code in C and make it functional? #include #include void printMaze(int maze[10][10], int posX, int posY) { int i; int j; printf("\033[0;0H\033[2J"); for (i = 0; i < 10; i++) { for (j = 0; j < 10; j++) { if (i == posX && j == posY) printf("? "); else { if (maze[i][j] == 0) printf(" _ "); else printf("? "); } } printf("\n"); } } void solveMaze(int maze[10][10], int x, int y) { printf("posX: %i, posY: %i\n", x, y); if (x == 9 && y == 9) { printf("*********Resuelto*********\n"); exit(0); } if (maze[x + 1][y] == 0) return solveMaze(maze, x += 1, y); if (maze[x][y + 1] == 0) return solveMaze(maze, x, y += 1); if (maze[x - 1][y] == 0) { if (maze[x - 1][y + 1] == 0) return solveMaze(maze, x -= 1, y += 1); } } int main(void) { int option = 0; int count = 0; int maze[10][10] = { {0,0,0,0,0,1,1,1,1,1}, {1,1,1,1,0,1,0,0,0,0}, {1,1,1,1,0,1,0,1,1,0}, {1,0,0,0,0,1,0,1,1,0}, {1,0,1,1,1,1,0,1,0,0}, {0,0,1,1,0,0,0,1,0,1}, {0,1,1,1,0,1,1,1,0,1}, {0,0,0,1,0,0,0,1,0,1}, {1,1,0,1,1,1,0,1,0,0}, {1,1,0,0,0,0,0,1,1,0}}; int posX = 0, posY = 0; solveMaze(maze, posX, posY); return 0; }
Can someone help me to improve the following code in C and make it functional?
#include <stdio.h>
#include <stdlib.h>
void printMaze(int maze[10][10], int posX, int posY)
{
int i;
int j;
printf("\033[0;0H\033[2J");
for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (i == posX && j == posY)
printf("? ");
else
{
if (maze[i][j] == 0)
printf(" _ ");
else
printf("? ");
}
}
printf("\n");
}
}
void solveMaze(int maze[10][10], int x, int y)
{
printf("posX: %i, posY: %i\n", x, y);
if (x == 9 && y == 9)
{
printf("*********Resuelto*********\n");
exit(0);
}
if (maze[x + 1][y] == 0)
return solveMaze(maze, x += 1, y);
if (maze[x][y + 1] == 0)
return solveMaze(maze, x, y += 1);
if (maze[x - 1][y] == 0)
{
if (maze[x - 1][y + 1] == 0)
return solveMaze(maze, x -= 1, y += 1);
}
}
int main(void)
{
int option = 0;
int count = 0;
int maze[10][10] = {
{0,0,0,0,0,1,1,1,1,1}, {1,1,1,1,0,1,0,0,0,0}, {1,1,1,1,0,1,0,1,1,0}, {1,0,0,0,0,1,0,1,1,0}, {1,0,1,1,1,1,0,1,0,0}, {0,0,1,1,0,0,0,1,0,1}, {0,1,1,1,0,1,1,1,0,1}, {0,0,0,1,0,0,0,1,0,1}, {1,1,0,1,1,1,0,1,0,0}, {1,1,0,0,0,0,0,1,1,0}};
int posX = 0,
posY = 0;
solveMaze(maze, posX, posY);
return 0;
}
Step by step
Solved in 3 steps with 2 images