diff --git a/MaxMin using divide and conquer.cpp b/MaxMin using divide and conquer.cpp new file mode 100644 index 0000000..3ca6e78 --- /dev/null +++ b/MaxMin using divide and conquer.cpp @@ -0,0 +1,53 @@ +#include +#include +using namespace std; +void in(int a[],int n){ + for(int i=0;i>a[i]; +} +void print(int a[],int n){ + for(int i=0;ib) + return a; + return b; +} +int getMin(int a,int b){ + if(a a,int n){ + for(int y=0;y MaxMin(int a[],int i,int n){ +// show(a,i,n); + if(i==n){ + vector h; + h.push_back(a[i]); + h.push_back(a[i]); + return h; + } + int mid=(i+n)/2; + vector jj; + vector m=MaxMin(a,i,mid); + vector k=MaxMin(a,mid+1,n); + jj.push_back(getMax(m[0],k[0])); + jj.push_back(getMin(m[1],k[1])); + return jj ; + +} +int main(void){ + int n; + cout<<"Size of the array-"; + cin>>n; + int a[n]; + cout<<"ENTER ELEMENTS-"; + in(a,n); + vector o=MaxMin(a,0,n-1); + cout<<"Max "< +#define list vector +using namespace std; +class nqueen{ + vector sol; + int n; + bool **b; + public: + nqueen(int v){ + n=v; + b=(bool**)malloc(n*sizeof(bool*)); + for(int i=0;i=0 and k=0 and k=0 and l>=0;k--,l--){ + if(b[k][l]) + return false; + } + for(int k=i,l=j;k=0;k++,l--){ + if(b[k][l]) + return false; + } + return true; + } + bool rec(int r,int c){ + if(c==n){ + list v; + for(int i=0;i>n; + nqueen q=nqueen(n); + q.soln(); +} diff --git a/rat in a Maze find all possible solution.cpp b/rat in a Maze find all possible solution.cpp new file mode 100644 index 0000000..2a31200 --- /dev/null +++ b/rat in a Maze find all possible solution.cpp @@ -0,0 +1,78 @@ +/* + + Consider a rat placed at (0, 0)in a square matrix of order N * N. + It has to reach the destination at (N - 1, N - 1). + Find all possible paths that the rat can take to reach from source to destination. The directions in which the rat can move are 'U'(up), 'D'(down), 'L' (left), 'R' (right). Value 0 at a cell in the matrix represents that it is blocked and rat cannot move to it while value 1 at a cell in the matrix represents that rat can be travel through it. + Note: In a path, no cell can be visited more than one time. + +*/ +#include +#define list vector +using namespace std; + + + // } Driver Code Ends +// User function template for C++ + +class Solution{ + public: + bool issafe(vector &m, int i,int j,int n){ + return (i=0 &&j>=0 && m[i][j]==1 ); + } + bool ss(vector &m,int i,int j,int n,string s,vector&res){ + if(i==n-1 and j==n-1 and m[i][j]==1){ + // s+="1"; + res.push_back(s); + return true; + } + if(issafe(m,i,j,n)) + + + { m[i][j]=0; + ss(m,i+1,j,n,s+'D',res); + ss(m,i-1,j,n,s+'U',res); + ss(m,i,j+1,n,s+'R',res); + ss(m,i,j-1,n,s+'L',res); + m[i][j]=1; + } + return false; + } + vector findPath(vector &m, int n) { + // Your code goes here + string s=""; + vector v; + ss(m,0,0,n,s,v); + + sort(v.begin(),v.end()); + // reverse(s.begin(),s.end()); + + // v.push_back(s); + return v; + } +}; + + + + +// { Driver Code Starts. + +int main() { + + int n; + cin >> n; + vector m(n, vector (n,0)); + for (int i = 0; i < n; i++) { + for (int j = 0; j < n; j++) { + cin >> m[i][j]; + } + } + Solution obj; + vector result = obj.findPath(m, n); + if (result.size() == 0) + cout << -1; + else + for (int i = 0; i < result.size(); i++) cout << result[i] << " "; + cout << endl; + + return 0; +} // } Driver Code Ends