The algorithm requests user to input rows(x) and columns(y) and grid’s value (grids value can be 0 or 1, if the value of the grid is 1 then it is blocked 0 means it is unblocked ) then it should provide the number of possible path to move from (1,1) to (x,y).
The problem is that my code is working fine. Still, it is not working as it was intended The algorithm is meant to move only to the right and down grid also it can jump over obstacles (Can jump over one grid), but for the input 3*3 grid, the input is 0 0 0, 0 1 1, 0 0 0 the output should contain the number of possible paths for this the output should be 3. It gives 3 but it is moving diagonally,
The path should be:can move from (1, 1) to (2, 1) and then to (3, 1) and finally to (3, 2) and then to (3, 3).
(3,2) is blocked so it should move from (3,1) to (3,3)
And another alternative is from (1, 1) to (2, 1) , and then to (2,3) and then to (3,3)
But the code consider the diagonal jump not the last path.
#include <stdio.h>
int countWays(int N, int M, int dp[N][M]) {
// Initialize the first cell
if (dp[0][0] == 0)
dp[0][0] = 1;
else
dp[0][0] = 0;
// Initialize the first row
for (int j = 1; j < M; j++) {
if (dp[0][j] == 0 && dp[0][j - 1] > 0)
dp[0][j] = 1;
else
dp[0][j] = 0;
}
// Initialize the first column
for (int i = 1; i < N; i++) {
if (dp[i][0] == 0 && dp[i - 1][0] > 0)
dp[i][0] = 1;
else
dp[i][0] = 0;
}
// Calculate the number of ways for each cell
for (int i = 1; i < N; i++) {
for (int j = 1; j < M; j++) {
if (dp[i][j] == 0) {
if (dp[i - 2][j] >= 0)
dp[i][j] += dp[i - 1][j];
if (dp[i][j - 1] >= 0)
dp[i][j] += dp[i][j - 1];
if (i > 0 && j > 1 && dp[i - 1][j - 2] >= 0)
dp[i][j] += dp[i - 1][j - 2]; // Jumping over one grid horizontally
if (i > 1 && j > 0 && dp[i - 1][j - 1] >= 0)
dp[i][j] += dp[i - 1][j - 1]; // Jumping over one grid vertically
} else {
dp[i][j] = 0; // If cell is blocked, mark 0
}
}
}
return dp[N - 1][M - 1];
}
int main() {
int N, M;
printf("Enter the number of rows: ");
scanf("%d", &N);
printf("Enter the number of columns: ");
scanf("%d", &M);
int dp[N][M];
printf("Enter the grid values (0 or 1):n");
// Input the grid values
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("Enter the value for row %d, column %d: ", i + 1, j + 1);
scanf("%d", &dp[i][j]);
}
}
int ways = countWays(N, M, dp);
printf("Number of ways to reach (%d, %d) from (1, 1): %dn", N, M, ways);
return 0;
}
enter image description here
enter image description here
As you can see it moves diagonal also but when the diagonal movement is restricted it gives the output as
#include <stdio.h>
int countWays(int N, int M, int dp[N][M]) {
// Initialize the first cell
if (dp[0][0] == 0)
dp[0][0] = 1;
else
dp[0][0] = 0;
// Initialize the first row
for (int j = 1; j < M; j++) {
if (dp[0][j] == 0 && dp[0][j - 1] > 0)
dp[0][j] = 1;
else
dp[0][j] = 0;
}
// Initialize the first column
for (int i = 1; i < N; i++) {
if (dp[i][0] == 0 && dp[i - 1][0] > 0)
dp[i][0] = 1;
else
dp[i][0] = 0;
}
// Calculate the number of ways for each cell
for (int i = 1; i < N; i++) {
for (int j = 1; j < M; j++) {
if (dp[i][j] == 0) {
if (dp[i - 1][j] >= 0)
dp[i][j] += dp[i - 1][j];
if (dp[i][j - 1] >= 0)
dp[i][j] += dp[i][j - 1];
if (i > 0 && j > 1 && dp[i - 1][j - 2] >= 0)
dp[i][j] += dp[i - 1][j - 2]; // Jumping over one grid horizontally
if (i > 1 && j > 0 && dp[i - 2][j - 1] >= 0)
dp[i][j] += dp[i - 2][j - 1]; // Jumping over one grid vertically
} else {
dp[i][j] = 0; // If cell is blocked, mark 0
}
}
}
return dp[N - 1][M - 1];
}
int main() {
int N, M;
printf("Enter the number of rows: ");
scanf("%d", &N);
printf("Enter the number of columns: ");
scanf("%d", &M);
int dp[N][M];
printf("Enter the grid values (0 or 1):n");
// Input the grid values
for (int i = 0; i < N; i++) {
for (int j = 0; j < M; j++) {
printf("Enter the value for row %d, column %d: ", i + 1, j + 1);
scanf("%d", &dp[i][j]);
}
}
int ways = countWays(N, M, dp);
printf("Number of ways to reach (%d, %d) from (1, 1): %dn", N, M, ways);
return 0;
}
enter image description here](https://i.sstatic.net/kqmYeeb8.png)
enter image description here
For input 33 , 0 0 0, 0 1 1, 0 0 0 output should be 3
For input 33 , 0 1 0, 1 0 0, 0 0 0 output should be 2
Colorless Hat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.