Daa 812
Daa 812
PRACTICAL – 8
AIM: Given a grid of dimension N x M where each cell in the grid can have
values 0, 1, or 2 which has the following meaning:
0: Empty cell
1: Cells have fresh oranges 2: Cells have rotten oranges
We have to determine what is the minimum time required to rot all oranges. A
rotten orange at index [i,j] can rot other fresh oranges at indexes [i-1,j], [i+1,j],
[i,j-1], [i,j+1] (up, down, left and right) in unit time’
CODE:
#include <stdio.h>
#include <stdlib.h>
// If there are still fresh oranges, return -1 (not possible to rot all)
if (freshOranges > 0) {
return -1;
}
return time;
}
int main() {
int N = 5, M = 5;
int grid[100][100] = {
{1, 1, 0, 0, 0},
{1, 1, 0, 0, 1},
{0, 1, 1, 1, 1},
{0, 0, 0, 1, 0},
{0, 0, 1, 1, 2}
};
return 0;
}
OUTPUT:
PRACTICAL – 9
AIM: Given two strings str1 and str2 and below operations that can be
performed on str1. Find minimum number of edits (operations) required to
convert ‘str1’ into ‘str2’. Insert Remove Replace, All of the above operations
are of equal cost.
CODE:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
// Function to find the minimum of three numbers
int min(int x, int y, int z) {
return x < y ? (x < z ? x : z) : (y < z ? y : z);
}
return 0;
}
OUTPUT:
PRACTICAL – 10
AIM: Minimum Path Sum” says that given a n x m grid consisting of non-
negative integers and we need to find a path from top-left to bottom right,
which minimizes the sum of all numbers along the path.
CODE:
#include <stdio.h>
#include <limits.h>
ENROLMENT NO: 2203031280003 Page | 30
PARUL UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY
SUBJECT: Design and Analysis Algorithm Laboratory
SUBJECT CODE:
303105306
B. TECH 3rd YEAR
}
// The answer is at the bottom-right corner of the dp table
return dp[n - 1][m - 1];
}
int main() {
int n, m;
printf("Enter the number of rows (n): ");
scanf("%d", &n);
printf("Enter the number of columns (m): ");
scanf("%d", &m);
int grid[100][100];
printf("Enter the grid values:\n");
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
scanf("%d", &grid[i][j]);
}
}
int result = minPathSum(grid, n, m);
printf("The minimum path sum is: %d\n", result);
return 0;
}
OUTPUT:
PRACTICAL – 11
AIM: Given string num representing a non-negative integer num, and an
integer k, return the smallest possible integer after removing k digits from
num.
CODE:
#include <stdio.h>
#include <string.h>
// Function to return the smallest possible integer after removing k digits
void removeKDigits(char* num, int k) {
int n = strlen(num);
char stack[n]; // Stack to store the resulting number
int top = 0; // Index for the stack
// Process each digit of the number
for (int i = 0; i < n; i++) {
// Remove digits from the stack if the current digit is smaller than the top
of the stack
while (top > 0 && stack[top - 1] > num[i] && k > 0) {
top--; // Pop the top of the stack
k--; // Decrease the number of digits to remove
}
stack[top++] = num[i]; // Push the current digit onto the stack
}
// If there are still digits to remove, remove from the end
top -= k;
return 0;
}
OUTPUT:
PRACTICAL – 12
AIM: There is a robot on an m x n grid. The robot is initially located at the top-
left corner (i.e., grid[0][0]).The robot tries to move to the bottom-right corner
(i.e., grid[m - 1][n - 1]). The robot can only move either down or right at any
point in time.Given the two integers m and n, return the number of possible
unique paths that the robot can take to reach the bottom-right corner.
CODE:
#include <stdio.h>
// Function to return the number of unique paths in an m x n grid
int uniquePaths(int m, int n) {
// Create a 2D dp array to store the number of paths for each cell
int dp[m][n];
// Initialize the first row and the first column
for (int i = 0; i < m; i++) {
dp[i][0] = 1; // Only one way to move in the first column (down only)
}
for (int j = 0; j < n; j++) {
dp[0][j] = 1; // Only one way to move in the first row (right only)
}
// Fill the dp array
OUTPUT: