From b2cd17e640a783bf74eca49236210b7421f7ae29 Mon Sep 17 00:00:00 2001 From: Atig Chkb <64916381+atig05@users.noreply.github.com> Date: Thu, 28 Oct 2021 12:50:35 +0530 Subject: [PATCH 1/5] Add files via upload find the maximum and minimum elements of an array using divide and conquer method --- MaxMin using divide and conquer.cpp | 53 +++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 MaxMin using divide and conquer.cpp 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 "< Date: Thu, 28 Oct 2021 12:57:08 +0530 Subject: [PATCH 2/5] Add files via upload 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. Example1 Input: N = 4 m[][] = {{1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 1}} Output: DDRDRR DRDDRR Explanation: The rat can reach the destination at (3, 3) from (0, 0) by two paths - DRDDRR and DDRDRR, when printed in sorted order we get DDRDRR DRDDRR. --- rat in a Maze find all possible solution.cpp | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 rat in a Maze find all possible solution.cpp 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 From d355d6f41bc4b5f089d02e9f1be5aae992e1aae2 Mon Sep 17 00:00:00 2001 From: Atig Chkb <64916381+atig05@users.noreply.github.com> Date: Thu, 28 Oct 2021 13:01:26 +0530 Subject: [PATCH 3/5] Delete rat in a Maze find all possible solution.cpp --- rat in a Maze find all possible solution.cpp | 78 -------------------- 1 file changed, 78 deletions(-) delete mode 100644 rat in a Maze find all possible solution.cpp diff --git a/rat in a Maze find all possible solution.cpp b/rat in a Maze find all possible solution.cpp deleted file mode 100644 index 2a31200..0000000 --- a/rat in a Maze find all possible solution.cpp +++ /dev/null @@ -1,78 +0,0 @@ -/* - - 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 From 6d662b94832c3c5af8448cc3e7dcea20dc762ea6 Mon Sep 17 00:00:00 2001 From: Atig Chkb <64916381+atig05@users.noreply.github.com> Date: Thu, 28 Oct 2021 13:05:29 +0530 Subject: [PATCH 4/5] Add files via upload 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. Example: Input: N = 4 m[][] = {{1, 0, 0, 0}, {1, 1, 0, 1}, {1, 1, 0, 0}, {0, 1, 1, 1}} Output: DDRDRR DRDDRR Explanation: The rat can reach the destination at (3, 3) from (0, 0) by two paths - DRDDRR and DDRDRR, when printed in sorted order we get DDRDRR DRDDRR. --- rat in a Maze find all possible solution.cpp | 78 ++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 rat in a Maze find all possible solution.cpp 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 From ec4bf35d35df7f6c94e8695822a4bf7888c359c8 Mon Sep 17 00:00:00 2001 From: Atig Chkb <64916381+atig05@users.noreply.github.com> Date: Thu, 28 Oct 2021 15:41:58 +0530 Subject: [PATCH 5/5] Add files via upload MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The N Queen is the problem of placing N chess queens on an N×N chessboard so that no two queens attack each other. This code takes the number of queens and outputs the possible solutions --- nqueen.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 nqueen.cpp diff --git a/nqueen.cpp b/nqueen.cpp new file mode 100644 index 0000000..c007fc8 --- /dev/null +++ b/nqueen.cpp @@ -0,0 +1,85 @@ +#include +#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(); +}