Problem:
-
Given a maze (grid), where:
-
From each traversable cell, find the shortest distance to any land mine.
-
Movement is allowed only in 4 directions: up, down, left, right.
Approach:
Use Multi-Source Breadth-First Search (BFS):
-
Treat all mines (-1
) as starting points.
-
From each mine, BFS and compute the distance to each cell.
-
BFS guarantees the shortest distance in an unweighted grid.
Algorithm (Step-by-Step):
-
Create a distance matrix initialized with -1
or infinity
.
-
Create a queue for BFS.
-
Enqueue all cells that are mines (-1
) with distance 0
.
-
While queue is not empty:
-
Done.
C Code for Shortest Distance to Land Mine
#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define INF 9999
// Directions: up, down, left, right
int dirRow[] = {-1, 1, 0, 0};
int dirCol[] = {0, 0, -1, 1};
struct Node {
int x, y;
};
struct Node queue[MAX*MAX];
int front = 0, rear = -1;
// Enqueue
void enqueue(int x, int y) {
rear++;
queue[rear].x = x;
queue[rear].y = y;
}
// Dequeue
struct Node dequeue() {
return queue[front++];
}
// Check if a cell is safe to visit
int isValid(int x, int y, int n, int m) {
return (x >= 0 && x < n && y >= 0 && y < m);
}
int main() {
int maze[MAX][MAX], dist[MAX][MAX];
int n, m;
printf("Enter number of rows and columns: ");
scanf("%d%d", &n, &m);
printf("Enter the maze (-1 for mine, 0 for path, # for wall as 999):\n");
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
scanf("%d", &maze[i][j]);
// Initialize distances
for (int i=0; i<n; i++)
for (int j=0; j<m; j++)
dist[i][j] = INF;
// Push all mines into the queue
for (int i=0; i<n; i++)
for (int j=0; j<m; j++) {
if (maze[i][j] == -1) {
enqueue(i, j);
dist[i][j] = 0;
}
}
// Multi-source BFS
while (front <= rear) {
struct Node temp = dequeue();
int x = temp.x;
int y = temp.y;
for (int k=0; k<4; k++) {
int newX = x + dirRow[k];
int newY = y + dirCol[k];
if (isValid(newX, newY, n, m) && maze[newX][newY] == 0 && dist[newX][newY] > dist[x][y] + 1) {
dist[newX][newY] = dist[x][y] + 1;
enqueue(newX, newY);
}
}
}
printf("Shortest distance from land mines:\n");
for (int i=0; i<n; i++) {
for (int j=0; j<m; j++) {
if (maze[i][j] == 999) printf(" # ");
else if (dist[i][j] == INF) printf(" INF ");
else printf("%3d ", dist[i][j]);
}
printf("\n");
}
return 0;
}
Output
Enter number of rows and columns: 5 5
Enter the maze (-1 for mine, 0 for path, # for wall as 999):
0 0 0 0 0
0 -1 0 0 0
0 0 0 0 -1
0 0 0 0 0
-1 0 0 0 999
Shortest distance from land mines:
2 1 2 3 2
1 0 1 2 1
2 1 2 1 0
1 2 3 2 1
0 1 2 3 #
Comments
Post a Comment