Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions Backtracking/rat_in_maze.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// C++ program to solve Rat in a Maze problem using
// backtracking
#include <bits/stdc++.h>
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<<" "<<sol[i][j]<<" ";
cout<<endl;
}
}

// A utility function to check if x, y is valid index for
// N*N maze
bool isSafe(int maze[N][N], int x, int y)
{
// if (x, y outside maze) return false
if (x >= 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;
}
6 changes: 6 additions & 0 deletions OOPS/hero.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Hero{
//Properties
char name[100];
int health;
char level;
};
114 changes: 114 additions & 0 deletions OOPS/oops1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#include<iostream>
//#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" <<endl;
}

// Parameterized Constructor
Hero(int health)
{
//cout<<"This -> "<<&health<<endl;
this->health = health;
}
Hero(int health,char level)
{
this->level = level;
this->health = health;
}
void print()
{
cout<<"Health is : "<<this->health<<endl;
cout<<"Level is : "<<this->level<<endl;
}

int getHealth(){
return health;
}
char getLevel(){
return level;
}
void setHealth(int h){
health = h;
}
void setLevel(char ch){
level =ch;
}
};

int main()
{

Hero suresh(70,'C');
suresh.print();

// Copy Constructor
Hero R(suresh);
R.print();

/*
// Object created Statically
cout << "Hi"<<endl;
Hero sutam(10);
cout<<"Hello"<<endl;

cout<<"Address of Sutam : "<< &sutam <<endl;

sutam.print();

//Dynamically
Hero *h = new Hero(11);
h->print();

Hero temp(22,'B');
h->print();*/

//Creation of Object
/*Hero h1;
h1.level = 'A';
cout << "size : "<<sizeof(h1)<<endl;

h1.setHealth(70);

cout<<"Level is : "<< h1.level<<endl; //As Private
cout<<"Health is : "<< h1.getHealth()<<endl;*/

/*
//Static Allocation
Hero a;

a.setHealth(70);
a.setLevel('A');

cout<<"Level is : "<<a.level<<endl;
cout<<"Health is : "<<a.getHealth()<<endl;

// Dynamic Allocation
Hero *b = new Hero; // Memory allocation in Heap

b->setHealth(80);
b->setLevel('B');

cout<<"Level is : "<<(*b).level<<endl;
cout<<"Health is : "<<(*b).getHealth()<<endl;
// OR
cout<<"Level is : "<<b->level<<endl;
cout<<"Health is : "<<b->getHealth()<<endl;

*/

return 0;
}
Binary file added OOPS/oops1.exe
Binary file not shown.
37 changes: 37 additions & 0 deletions Pattern Problem/dabang_pattern.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
1 2 3 4 5 5 4 3 2 1
1 2 3 4 * * 4 3 2 1
1 2 3 * * * * 3 2 1
1 2 * * * * * * 2 1
1 * * * * * * * * 1
*/

#include<iostream>
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<<i<<"\t";

/* 1st Space Angle */
for(int j=row-1;j>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<<k<<"\t";

cout<<endl;
}
return 0;
}
Binary file added Pattern Problem/dabang_pattern.exe
Binary file not shown.
27 changes: 27 additions & 0 deletions Pattern Problem/problem_1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
1 2 3 4
1 2 3 4
1 2 3 4
1 2 3 4
*/



#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter the Rows : ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<j+1<<"\t";
}
cout<<"\n";
}

return 0;
}
Binary file added Pattern Problem/problem_1.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions Pattern Problem/problem_10.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;
int main()
{
int n=0;
cout<<"Enter the no of rows : ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=n-i;j>0;j--)
{
cout<<"*"<<"\t";
}
cout<<"\n";
}
return 0;
}
Binary file added Pattern Problem/problem_10.exe
Binary file not shown.
19 changes: 19 additions & 0 deletions Pattern Problem/problem_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include<iostream>
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<<endl;
}
return 0;
}
Binary file added Pattern Problem/problem_11.exe
Binary file not shown.
24 changes: 24 additions & 0 deletions Pattern Problem/problem_12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include<iostream>
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<<k<<"\t";

/* 2nd Triangle */
for(int l=i-1;l>0;l--)
cout<<l<<"\t";
cout<<endl;
}
return 0;
}
Binary file added Pattern Problem/problem_12.exe
Binary file not shown.
18 changes: 18 additions & 0 deletions Pattern Problem/problem_2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include<iostream>
using namespace std;
int main()
{
int n;
cout<<"Enter Rows number : ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=n-1;j>=0;j--)
{
cout<<j+1<<"\t";
}
cout<<"\n";
}

return 0;
}
Binary file added Pattern Problem/problem_2.exe
Binary file not shown.
17 changes: 17 additions & 0 deletions Pattern Problem/problem_3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include<iostream>
using namespace std;
int main()
{
int n =0 ;
cout<<"Enter the no of rows : ";
cin>>n;
for(int i=0;i<n;i++)
{
for(int j=0;j<=i;j++)
{
cout<<"*\t";
}
cout<<endl;
}
return 0;
}
Binary file added Pattern Problem/problem_3.exe
Binary file not shown.
Loading