From c218439f0636e3d50b962ae84898d19315df2590 Mon Sep 17 00:00:00 2001 From: Kshitij Alwadhi <52427677+kshitijalwadhi@users.noreply.github.com> Date: Fri, 2 Oct 2020 00:22:17 +0530 Subject: [PATCH] Create ways_maze.cpp --- dynamic_programming/ways_maze.cpp | 81 +++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 dynamic_programming/ways_maze.cpp diff --git a/dynamic_programming/ways_maze.cpp b/dynamic_programming/ways_maze.cpp new file mode 100644 index 0000000..09730d6 --- /dev/null +++ b/dynamic_programming/ways_maze.cpp @@ -0,0 +1,81 @@ + +// C++ program to count number of paths in a maze +// with obstacles. +#include +using namespace std; +#define R 4 +#define C 4 + +// Returns count of possible paths in a maze[R][C] +// from (0,0) to (R-1,C-1) +int countPaths(int maze[][C]) +{ + // If the initial cell is blocked, there is no + // way of moving anywhere + if (maze[0][0]==-1) + return 0; + + // Initializing the leftmost column + for (int i=0; i 0) + maze[i][j] = (maze[i][j] + maze[i-1][j]); + + // If we can reach maze[i][j] from maze[i][j-1] + // then increment count. + if (maze[i][j-1] > 0) + maze[i][j] = (maze[i][j] + maze[i][j-1]); + } + } + + // If the final cell is blocked, output 0, otherwise + // the answer + return (maze[R-1][C-1] > 0)? maze[R-1][C-1] : 0; +} + +// Driver code +int main() +{ + int maze[R][C] = {{0, 0, 0, 0}, + {0, -1, 0, 0}, + {-1, 0, 0, 0}, + {0, 0, 0, 0}}; + cout << countPaths(maze); + return 0; +}