diff --git a/Backtracking/rat_in_maze.cpp b/Backtracking/rat_in_maze.cpp new file mode 100644 index 0000000..6056405 --- /dev/null +++ b/Backtracking/rat_in_maze.cpp @@ -0,0 +1,92 @@ +// C++ program to solve Rat in a Maze problem using +// backtracking +#include +using namespace std; +#define N 4 + +bool solveMazeUtil(int maze[N][N], int x, int y,int sol[N][N]); + +// A utility function to print solution matrix sol[N][N] +void printSolution(int sol[N][N]) +{ + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) + cout<<" "<= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1) + return true; + return false; +} + + +bool solveMaze(int maze[N][N]) +{ + int sol[N][N] = { { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 }, + { 0, 0, 0, 0 } }; + if (solveMazeUtil(maze, 0, 0, sol) == false) { + cout<<"Solution doesn't exist"; + return false; + } + printSolution(sol); + return true; +} + +// A recursive utility function to solve Maze problem +bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]) +{ + // if (x, y is goal) return true + if (x == N - 1 && y == N - 1 && maze[x][y] == 1) { + sol[x][y] = 1; + return true; + } + // Check if maze[x][y] is valid + if (isSafe(maze, x, y) == true) { + // Check if the current block is already part of + // solution path. + if (sol[x][y] == 1) + return false; + // mark x, y as part of solution path + sol[x][y] = 1; + /* Move forward in x direction */ + if (solveMazeUtil(maze, x + 1, y, sol) == true) + return true; + // If moving right didn't work + // move left + if (solveMazeUtil(maze, x - 1, y, sol) == true) + return true; + // If moving in x direction doesn't give solution + // then Move down in y direction + if (solveMazeUtil(maze, x, y + 1, sol) == true) + return true; + // If moving down didn't work + // move up + if (solveMazeUtil(maze, x, y - 1, sol) == true) + return true; + // If none of the above movements work then + // BACKTRACK: unmark x, y as part of solution path + sol[x][y] = 0; + return false; + } + return false; +} + +// driver program to test above function +int main() +{ + int maze[N][N] = { { 1, 0, 0, 0 }, + { 1, 1, 0, 1 }, + { 0, 1, 0, 0 }, + { 1, 1, 1, 1 } }; + solveMaze(maze); + return 0; +} \ No newline at end of file diff --git a/OOPS/hero.cpp b/OOPS/hero.cpp new file mode 100644 index 0000000..e6bc851 --- /dev/null +++ b/OOPS/hero.cpp @@ -0,0 +1,6 @@ +class Hero{ + //Properties + char name[100]; + int health; + char level; +}; \ No newline at end of file diff --git a/OOPS/oops1.cpp b/OOPS/oops1.cpp new file mode 100644 index 0000000..f7183eb --- /dev/null +++ b/OOPS/oops1.cpp @@ -0,0 +1,114 @@ +#include +//#include "hero.cpp" +using namespace std; + +class Hero{ + //Properties + + private: // we can use this globally.. + //char name[100]; + int health; + + + // In case of private we access this in the class only.. + public: + char level; + + Hero(){ + cout << "Constructor called" < "<<&health<health = health; + } + Hero(int health,char level) + { + this->level = level; + this->health = health; + } + void print() + { + cout<<"Health is : "<health<level<print(); + + Hero temp(22,'B'); + h->print();*/ + + //Creation of Object + /*Hero h1; + h1.level = 'A'; + cout << "size : "<setHealth(80); + b->setLevel('B'); + + cout<<"Level is : "<<(*b).level<level<getHealth()< +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int row=1;row<=n;row++) + { + /* 1st Triangle */ + for(int i=1;i<=n-row+1;i++) + cout<0;j--) + cout<<"*"<<"\t"; + + /* 2nd Space angle */ + for(int l=row-1;l>=1;l--) + cout<<"*"<<"\t"; + + /* 2nd Triangle */ + for(int k=n-row+1;k>=1;k--) + cout< +using namespace std; +int main() +{ + int n; + cout<<"Enter the Rows : "; + cin>>n; + for(int i=0;i +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=0;i0;j--) + { + cout<<"*"<<"\t"; + } + cout<<"\n"; + } + return 0; +} \ No newline at end of file diff --git a/Pattern Problem/problem_10.exe b/Pattern Problem/problem_10.exe new file mode 100644 index 0000000..d41562c Binary files /dev/null and b/Pattern Problem/problem_10.exe differ diff --git a/Pattern Problem/problem_11.cpp b/Pattern Problem/problem_11.cpp new file mode 100644 index 0000000..ab231dd --- /dev/null +++ b/Pattern Problem/problem_11.cpp @@ -0,0 +1,19 @@ +#include +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=1;i<=n;i++) + { + /* space */ + for(int j=i-1;j>0;j--) + cout<<" "<<"\t"; + + for(int k= n-i+1;k>0;k--) + cout<<"*\t"; + cout< +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=1;i<=n;i++) + { + /* Spaces */ + for(int j=n-i;j>0;j--) + cout<<" "<<"\t"; + /* 1st TriAngle */ + + for(int k=1;k<=i;k++) + cout<0;l--) + cout< +using namespace std; +int main() +{ + int n; + cout<<"Enter Rows number : "; + cin>>n; + for(int i=0;i=0;j--) + { + cout< +using namespace std; +int main() +{ + int n =0 ; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=0;i +using namespace std; +int main() +{ + int n =0 ; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=0;i +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=1;i<=n;i++) + { + int k = i; + for(int j=1;j<=i;j++) + { + cout< +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + int k = 'A'; + for(int i=0;i +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + int k = 'A'; + for(int i=0;i +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + int k = 'A'; + for(int i=0;i +using namespace std; +int main() +{ + int n=0; + cout<<"Enter the no of rows : "; + cin>>n; + for(int i=1;i<=n;i++) + { + for(int j=n-i;j;j--) + { + cout<<" "<<"\t"; + } + for(int k=1;k<=i;k++) + { + cout<<"*"<<"\t"; + } + cout<