Here's an efficient C implementation using A* pathfinding for your 20x10 grid:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define GRID_WIDTH 20
#define GRID_HEIGHT 10
#define MAX_NODES (GRID_WIDTH * GRID_HEIGHT)
typedef struct {
int x, y;
int g_cost; // Distance from start
int h_cost; // Heuristic distance to goal
int f_cost; // g_cost + h_cost
int parent_x, parent_y;
int in_open_set;
int in_closed_set;
} Node;
typedef struct {
int x, y;
} Point;
// Grid: 0 = walkable, 1 = obstacle
static int grid[GRID_HEIGHT][GRID_WIDTH];
static Node nodes[GRID_HEIGHT][GRID_WIDTH];
// Simple priority queue using array (efficient for small grids)
static Point open_set[MAX_NODES];
static int open_count = 0;
// Manhattan distance heuristic
static inline int heuristic(int x1, int y1, int x2, int y2) {
return abs(x1 - x2) + abs(y1 - y2);
}
// Add to open set maintaining min-heap property
static void add_to_open_set(int x, int y) {
open_set[open_count].x = x;
open_set[open_count].y = y;
nodes[y][x].in_open_set = 1;
// Bubble up
int current = open_count;
open_count++;
while (current > 0) {
int parent = (current - 1) / 2;
if (nodes[open_set[current].y][open_set[current].x].f_cost >=
nodes[open_set[parent].y][open_set[parent].x].f_cost) break;
Point temp = open_set[current];
open_set[current] = open_set[parent];
open_set[parent] = temp;
current = parent;
}
}
// Extract minimum from open set
static Point extract_min() {
Point min_node = open_set[0];
nodes[min_node.y][min_node.x].in_open_set = 0;
open_count--;
if (open_count > 0) {
open_set[0] = open_set[open_count];
// Bubble down
int current = 0;
while (1) {
int left = 2 * current + 1;
int right = 2 * current + 2;
int smallest = current;
if (left < open_count &&
nodes[open_set[left].y][open_set[left].x].f_cost <
nodes[open_set[smallest].y][open_set[smallest].x].f_cost) {
smallest = left;
}
if (right < open_count &&
nodes[open_set[right].y][open_set[right].x].f_cost <
nodes[open_set[smallest].y][open_set[smallest].x].f_cost) {
smallest = right;
}
if (smallest == current) break;
Point temp = open_set[current];
open_set[current] = open_set[smallest];
open_set[smallest] = temp;
current = smallest;
}
}
return min_node;
}
// Find shortest path using A*
int find_path(int start_x, int start_y, int goal_x, int goal_y, Point* path) {
// Bounds checking
if (start_x < 0 || start_x >= GRID_WIDTH || start_y < 0 || start_y >= GRID_HEIGHT ||
goal_x < 0 || goal_x >= GRID_WIDTH || goal_y < 0 || goal_y >= GRID_HEIGHT ||
grid[start_y][start_x] == 1 || grid[goal_y][goal_x] == 1) {
return -1;
}
// Reset nodes
memset(nodes, 0, sizeof(nodes));
open_count = 0;
// Initialize start node
nodes[start_y][start_x].g_cost = 0;
nodes[start_y][start_x].h_cost = heuristic(start_x, start_y, goal_x, goal_y);
nodes[start_y][start_x].f_cost = nodes[start_y][start_x].h_cost;
nodes[start_y][start_x].parent_x = -1;
nodes[start_y][start_x].parent_y = -1;
add_to_open_set(start_x, start_y);
// Directions: up, right, down, left
int dx[] = {0, 1, 0, -1};
int dy[] = {-1, 0, 1, 0};
while (open_count > 0) {
Point current = extract_min();
int curr_x = current.x;
int curr_y = current.y;
nodes[curr_y][curr_x].in_closed_set = 1;
// Goal reached
if (curr_x == goal_x && curr_y == goal_y) {
// Reconstruct path
int path_length = 0;
int x = goal_x, y = goal_y;
while (x != -1 && y != -1) {
path[path_length].x = x;
path[path_length].y = y;
path_length++;
int temp_x = nodes[y][x].parent_x;
int temp_y = nodes[y][x].parent_y;
x = temp_x;
y = temp_y;
}
// Reverse path
for (int i = 0; i < path_length / 2; i++) {
Point temp = path[i];
path[i] = path[path_length - 1 - i];
path[path_length - 1 - i] = temp;
}
return path_length;
}
// Check neighbors
for (int i = 0; i < 4; i++) {
int next_x = curr_x + dx[i];
int next_y = curr_y + dy[i];
// Bounds check and obstacle check
if (next_x < 0 || next_x >= GRID_WIDTH ||
next_y < 0 || next_y >= GRID_HEIGHT ||
grid[next_y][next_x] == 1 ||
nodes[next_y][next_x].in_closed_set) {
continue;
}
int tentative_g = nodes[curr_y][curr_x].g_cost + 1;
if (!nodes[next_y][next_x].in_open_set) {
nodes[next_y][next_x].g_cost = tentative_g;
nodes[next_y][next_x].h_cost = heuristic(next_x, next_y, goal_x, goal_y);
nodes[next_y][next_x].f_cost = tentative_g + nodes[next_y][next_x].h_cost;
nodes[next_y][next_x].parent_x = curr_x;
nodes[next_y][next_x].parent_y = curr_y;
add_to_open_set(next_x, next_y);
} else if (tentative_g < nodes[next_y][next_x].g_cost) {
nodes[next_y][next_x].g_cost = tentative_g;
nodes[next_y][next_x].f_cost = tentative_g + nodes[next_y][next_x].h_cost;
nodes[next_y][next_x].parent_x = curr_x;
nodes[next_y][next_x].parent_y = curr_y;
}
}
}
return -1; // No path found
}
// Example usage
int main() {
// Initialize grid (0 = walkable, 1 = obstacle)
memset(grid, 0, sizeof(grid));
// Add some obstacles
grid[2][5] = 1;
grid[3][5] = 1;
grid[4][5] = 1;
grid[5][10] = 1;
grid[6][10] = 1;
Point path[MAX_NODES];
int path_length = find_path(0, 0, 19, 9, path);
if (path_length > 0) {
printf("Path found with length %d:\n", path_length);
for (int i = 0; i < path_length; i++) {
printf("(%d, %d) ", path[i].x, path[i].y);
}
printf("\n");
} else {
printf("No path found\n");
}
return 0;
}
This implementation is optimized for speed with several key features:
Performance optimizations:
- Uses static arrays to avoid dynamic allocation overhead
- Implements a binary heap for the open set with O(log n) operations
- Uses Manhattan distance heuristic (optimal for orthogonal grids)
- Minimal memory allocations and efficient data structures
- Inline functions for frequently called operations
Key functions:
find_path(): Main pathfinding function returning path length (-1 if no path)
add_to_open_set() / extract_min(): Efficient priority queue operations
- Grid is represented as a 2D array where 0 = walkable, 1 = obstacle
The algorithm typically runs in microseconds for a 20x10 grid. For even better performance on repeated searches, you could implement jump point search or hierarchical pathfinding, but A* should be more than sufficient for your grid size.