diff --git a/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/Readme.md b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/Readme.md new file mode 100644 index 00000000..230ec4c3 --- /dev/null +++ b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/Readme.md @@ -0,0 +1,75 @@ +# Battery Usage Optimization for Robots in a Grid + +## Description +This program optimizes battery usage for a robot navigating a grid. The robot starts at the top-left corner and aims to reach the bottom-right corner while avoiding obstacles. The user can define the grid size and layout through input. + +## Features +- Dynamic grid size input (up to a defined maximum). +- Input for grid values where: + - `0` represents free space. + - `1` represents obstacles. +- Outputs the path taken by the robot, including each step. + +## Requirements +- C compiler (e.g., GCC) +- Standard C library + +## Compilation and Execution +1. **Clone the repository** (if using a version control system) or download the source code file `optimize_battery_with_input.c`. +2. Open your terminal and navigate to the directory containing the source code. +3. Compile the program using: + ```bash + gcc optimize_battery_with_input.c -o optimize_battery_with_input + +Run the executable: +bash +Copy code +./optimize_battery_with_input +Input Format +Grid Size: Enter the grid size (integer value). +Grid Values: Enter the grid values row by row, where: +0 indicates free space. +1 indicates an obstacle. +Example Input +java +Copy code +Enter grid size (max 10): 5 +Enter grid values (0 for free, 1 for obstacle): +0 0 0 0 1 +0 1 0 0 1 +0 1 0 1 1 +0 0 0 0 0 +1 1 1 1 0 +Output +The program will display the grid and the steps taken by the robot to reach the destination. If a valid path is found, it will show each move. + +Example Output +java +Copy code +Grid: +0 0 0 0 1 +0 1 0 0 1 +0 1 0 1 1 +0 0 0 0 0 +1 1 1 1 0 +Starting optimization from (0, 0): +Moving to (0, 1) +Moving to (0, 2) +Moving to (0, 3) +Moving to (1, 3) +Moving to (2, 3) +Moving to (3, 3) +Moving to (4, 3) +Moving to (4, 4) +Reached destination at (4, 4) +License +This project is licensed under the MIT License - see the LICENSE file for details. + +Acknowledgments +Thanks to the open-source community for their invaluable contributions. +css +Copy code + +### Usage +- Copy the above content and save it as `README.md` in your project directory. +- This Markdown file provides a clear overview of your program, including its features, requirements, inp \ No newline at end of file diff --git a/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.c b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.c new file mode 100644 index 00000000..714ef654 --- /dev/null +++ b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.c @@ -0,0 +1,96 @@ +#include + +#define MAX_SIZE 10 // Maximum grid size + +// Function to display the grid +void displayGrid(int grid[MAX_SIZE][MAX_SIZE], int size) +{ + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + printf("%d ", grid[i][j]); + } + printf("\n"); + } +} + +// Function to find the optimal path +int optimizePath(int grid[MAX_SIZE][MAX_SIZE], int size, int x, int y) +{ + // Base case: If robot reaches the bottom-right corner + if (x == size - 1 && y == size - 1) + { + printf("Reached destination at (%d, %d)\n", x, y); + return 1; // Return 1 to indicate a valid path found + } + + // Move right if possible + if (y + 1 < size) + { + printf("Moving to (%d, %d)\n", x, y + 1); + if (optimizePath(grid, size, x, y + 1)) + { + return 1; // Return 1 if path is found + } + } + + // Move down if possible + if (x + 1 < size) + { + printf("Moving to (%d, %d)\n", x + 1, y); + if (optimizePath(grid, size, x + 1, y)) + { + return 1; // Return 1 if path is found + } + } + + return 0; // Return 0 if no path is found +} + +int main() +{ + int grid[MAX_SIZE][MAX_SIZE]; + int size; + + // Get grid size from user + printf("Enter grid size (max %d): ", MAX_SIZE); + scanf("%d", &size); + + // Validate grid size + if (size <= 0 || size > MAX_SIZE) + { + printf("Invalid grid size!\n"); + return 1; + } + + // Get grid values from user + printf("Enter grid values (0 for free, 1 for obstacle):\n"); + for (int i = 0; i < size; i++) + { + for (int j = 0; j < size; j++) + { + scanf("%d", &grid[i][j]); + } + } + + // Display the grid + printf("Grid:\n"); + displayGrid(grid, size); + + // Check if the start or end point is blocked + if (grid[0][0] == 1 || grid[size - 1][size - 1] == 1) + { + printf("Start or destination is blocked!\n"); + return 1; + } + + // Start optimizing path from (0, 0) + printf("Starting optimization from (0, 0):\n"); + if (!optimizePath(grid, size, 0, 0)) + { + printf("No valid path found.\n"); + } + + return 0; +} diff --git a/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.exe b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.exe new file mode 100644 index 00000000..2e6cc219 Binary files /dev/null and b/Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.exe differ