To compute the shortest path on a 2D orthogonal grid (20x10) with obstacles, you can use Breadth-First Search (BFS), which is optimal for unweighted grids. Since you're looking for speed and efficiency, we'll use an optimized queue-based BFS with a static grid and direction vectors.
✅ Features:
Grid size: ROWS = 10, COLS = 20
Obstacles marked as 1, free cells as 0
Start and end positions are user-defined
Uses BFS for shortest path
Uses a circular queue for memory efficiency
Avoids recursion and uses minimal memory
✅ C Code (Fast and Efficient BFS):
c
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define ROWS 10
#define COLS 20
#define QUEUE_SIZE (ROWS * COLS)
// Direction vectors for up, down, left, right
const int dx[4] = {-1, 1, 0, 0};
const int dy[4] = {0, 0, -1, 1};
// Queue element
typedef struct {
int x, y, dist;
} Cell;
// Queue structure (circular)
typedef struct {
int front, rear, size;
Cell* items;
} Queue;
// Create queue
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->items = (Cell*)malloc(QUEUE_SIZE * sizeof(Cell));
q->front = 0;
q->rear = -1;
q->size = 0;
return q;
}
// Enqueue
void enqueue(Queue* q, Cell c) {
q->rear = (q->rear + 1) % QUEUE_SIZE;
q->items[q->rear] = c;
q->size++;
}
// Dequeue
Cell dequeue(Queue* q) {
Cell c = q->items[q->front];
q->front = (q->front + 1) % QUEUE_SIZE;
q->size--;
return c;
}
// Check if queue is empty
bool isEmpty(Queue* q) {
return q->size == 0;
}
// Free queue
void freeQueue(Queue* q) {
free(q->items);
free(q);
}
// BFS to find shortest path
int shortestPath(int grid[ROWS][COLS], int startX, int startY, int endX, int endY) {
if (grid[startX][startY] == 1 || grid[endX][endY] == 1) {
printf("Start or end is blocked.\n");
return -1;
}
bool visited[ROWS][COLS];
memset(visited, false, sizeof(visited));
Queue* q = createQueue();
Cell start = {startX, startY, 0};
enqueue(q, start);
visited[startX][startY] = true;
while (!isEmpty(q)) {
Cell curr = dequeue(q);
if (curr.x == endX && curr.y == endY) {
freeQueue(q);
return curr.dist;
}
for (int i = 0; i < 4; i++) {
int nx = curr.x + dx[i];
int ny = curr.y + dy[i];
if (nx >= 0 && nx < ROWS && ny >= 0 && ny < COLS &&
grid[nx][ny] == 0 && !visited[nx][ny]) {
visited[nx][ny] = true;
Cell next = {nx, ny, curr.dist + 1};
enqueue(q, next);
}
}
}
freeQueue(q);
printf("No path found.\n");
return -1; // No path
}
// Example usage
int main() {
// Example grid: 0 = free, 1 = obstacle
int grid[ROWS][COLS] = {
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}
};
int startX = 0, startY = 0;
int endX = 9, endY = 19;
int distance = shortestPath(grid, startX, startY, endX, endY);
if (distance != -1)
printf("Shortest path length: %d\n", distance);
else
printf("No valid path exists.\n");
return 0;
}
🔍 How It Works:
BFS explores all reachable neighbors layer by layer.
Uses a circular queue to manage memory efficiently.
Visited matrix prevents revisiting nodes.
Returns the shortest distance or -1 if no path exists.
🧪 Test Cases:
You can modify the grid to place obstacles and test different start/end points.
🚀 Optimization Notes:
Uses only O(rows * cols) memory.
Avoids recursion (stack overflow risk).
BFS is optimal for unweighted grids.
Can be further optimized with early exits or directional pruning.