Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimizing Battery Usage for Robots in Grid #1306

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Greedy Algorithms/Optimal Battery Usage for Robotic Grid/Readme.md
Original file line number Diff line number Diff line change
@@ -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
96 changes: 96 additions & 0 deletions Greedy Algorithms/Optimal Battery Usage for Robotic Grid/program.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include <stdio.h>

#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;
}
Binary file not shown.