Grid Traversal Algorithm in C

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 3
3 , 0 1 0, 1 0 0, 0 0 0 output should be 2

New contributor

Colorless Hat is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật