Hi All,

This is the code of the recursive method. Thank you all for your reply and take time to help me improve my C programming skill.

Java code:

Code:

public static boolean completePath(int[][] data) {
for (int[] dataVal : data) {
for (int value : dataVal) {
if (value == 0) {
return false;
}
}
}
return true;
}
public static int[][] cloneData(int[][] data) {
int[][] cloneData = new int[data.length][data[0].length];
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[0].length; j++) {
cloneData[i][j] = data[i][j];
}
}
return cloneData;
}
public static long readyGo(int y, int x, int[][] grid) {
long count = 0;
if (y != 0) {
if (grid[y - 1][x] == 3) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
if (completePath(cloneGrid)) {
return 1;
}
}
if (grid[y - 1][x] == 0) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
count += readyGo(y - 1, x, cloneGrid);
}
}
if (y != grid.length - 1) {
if (grid[y + 1][x] == 3) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
if (completePath(cloneGrid)) {
return 1;
}
}
if (grid[y + 1][x] == 0) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
count += readyGo(y + 1, x, cloneGrid);
}
}
if (x != 0) {
if (grid[y][x - 1] == 3) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
if (completePath(cloneGrid)) {
return 1;
}
}
if (grid[y][x - 1] == 0) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
count += readyGo(y, x - 1, cloneGrid);
}
}
if (x != grid[0].length - 1) {
if (grid[y][x + 1] == 3) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
if (completePath(cloneGrid)) {
return 1;
}
}
if (grid[y][x + 1] == 0) {
int[][] cloneGrid = cloneData(grid);
cloneGrid[y][x] = 1;
count += readyGo(y, x + 1, cloneGrid);
}
}
return count;
}

And this is my C program codes:

Code:

// Global variable.
int width = 0, height = 0;
char **guide_ptr;
int complete_path(char **grid_graph) {
for (int i = 0; i < height; i++) {
char *char_ptr = strchr(grid_graph[i], '0');
if (char_ptr != NULL) {
return 0;
}
}
return 1;
}
char **clone_data(char **grid_graph) {
char **clone_grid = calloc(sizeof(char *), height);
for (int i = 0; i < height; i++) {
clone_grid[i] = malloc(width);
memcpy(clone_grid[i], grid_graph[i], width);
}
return clone_grid;
}
void free_data(char **grid_graph) {
for (int i = 0; i < height; i++) {
free(grid_graph[i]);
}
free(grid_graph);
}
// recursive method
long readyGo(int x, int y, char **grid_graph) {
long count = 0;
if (y != 0) {
if (*(grid_graph[y-1] + x) == '3') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
if (complete_path(clone_graph)) {
free_data(clone_graph);
return 1;
}
free_data(clone_graph);
}
if (*(grid_graph[y-1] + x) == '0') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
count += readyGo(x, y - 1, clone_graph);
free_data(clone_graph);
}
}
if (y != height - 1) {
if (*(grid_graph[y+1] + x) == '3') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
if (complete_path(clone_graph)) {
free_data(clone_graph);
return 1;
}
free_data(clone_graph);
}
if (*(grid_graph[y+1] + x) == '0') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
count += readyGo(x, y + 1, clone_graph);
free_data(clone_graph);
}
}
if (x != 0) {
if (*(grid_graph[y] + x - 1) == '3') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
if (complete_path(clone_graph)) {
free_data(clone_graph);
return 1;
}
free_data(clone_graph);
}
if (*(grid_graph[y] + x - 1) == '0') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
count += readyGo(x - 1, y, clone_graph);
free_data(clone_graph);
}
}
if (x != width - 1) {
if (*(grid_graph[y] + x + 1) == '3') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
if (complete_path(clone_graph)) {
free_data(clone_graph);
return 1;
}
free_data(clone_graph);
}
if (*(grid_graph[y] + x + 1) == '0') {
char **clone_graph = clone_data(grid_graph);
*(clone_graph[y] + x) = '1';
count += readyGo(x + 1, y, clone_graph);
free_data(clone_graph);
}
}
return count;
}