So I have to scan a maze number of rows and columns for a file, bit I got that part.
File have a form like this
3
4
....
.#.#
....
Where the first number is a number of rows and the second is the number of columns. Character '#' is a wall and I can't go there but I can go through '.' Now I have to, using structures and pointers, find a shortest path to any point in a maze. Example structure is in my code (Cell).
I have no idea how to do this. I've created a array 'visited' to track which cell I've been to and function to check if a point is valid.
Somehow I have to point to other points in NORTH, WEST, EAST, SOUTH directions.
#include "stdafx.h"
#include "stdlib.h"
//Starting point
#define START_X 0
#define START_Y 0
//example structure I have to use
struct Cell
{
struct Cell *north;
struct Cell *east;
struct Cell *south;
struct Cell *west;
char value;
int distance;
};
//function that prints a maze
void printMap(char **charMaze, int row, int col)
{
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
printf_s("%c", charMaze[i][j]);
}
printf_s("\n");
}
}
// functions that check if a point is valid
bool isValid(int x, int y, int row, int col)
{
if (x < row && y < col && x >= 0 && y >= 0)
return true;
return false;
}
bool isSafe(char **charMaze, int **visited, int x, int y)
{
if (charMaze[x][y] == '#' || visited[x][y]==true)
return false;
return true;
}
//My attempt at solving this
int BFS(char **maze,int END_X, int END_Y,int row, int col, bool **visited)
{
isValid(END_X, END_Y, row, col);
}
int main()
{
FILE *map;
int row, col;
// I open a file with a maze
fopen_s(&map, "test1.txt", "r");
// I scan a row number and column number
fscanf_s(map, "%d", &row);
fscanf_s(map, "\n%d\n", &col);
char** charMaze;
charMaze = (char**)malloc(row * sizeof(char*));
for (int i = 0; i < row; i++)
charMaze[i] =(char*)malloc(col * sizeof(char));
bool** visited;
visited = (bool**)malloc(row * sizeof(bool*));
for (int i = 0; i < row; i++)
visited[i] = (bool*)malloc(col * sizeof(bool));
//set staring point as true and other points as false
visited[START_X][START_Y] = true;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
visited[i][j] = false;
}
}
// I scan a maze and I put it in a array
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
fscanf_s(map, "%c", &charMaze[i][j],1);
}
fscanf_s(map, "\n");
}
fclose(map);
//printMap(charMaze, row, col);
return 0;
}