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
53 changes: 53 additions & 0 deletions MaxMin using divide and conquer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#include<iostream>
#include<vector>
using namespace std;
void in(int a[],int n){
for(int i=0;i<n;i++)
cin>>a[i];
}
void print(int a[],int n){
for(int i=0;i<n;i++)
cout<<a[i];
}
int getMax(int a,int b){
if(a>b)
return a;
return b;
}
int getMin(int a,int b){
if(a<b)
return a;
return b;
}
void show(vector<int> a,int n){
for(int y=0;y<n;y++)
cout<<a[y]<<" ";
cout<<endl;
}
vector<int> MaxMin(int a[],int i,int n){
// show(a,i,n);
if(i==n){
vector<int> h;
h.push_back(a[i]);
h.push_back(a[i]);
return h;
}
int mid=(i+n)/2;
vector<int> jj;
vector<int> m=MaxMin(a,i,mid);
vector<int> 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<int> o=MaxMin(a,0,n-1);
cout<<"Max "<<o[0]<<endl<<"Min "<<o[1];
}
85 changes: 85 additions & 0 deletions nqueen.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include<bits/stdc++.h>
#define list vector<int>
using namespace std;
class nqueen{
vector<list> sol;
int n;
bool **b;
public:
nqueen(int v){
n=v;
b=(bool**)malloc(n*sizeof(bool*));
for(int i=0;i<n;i++){
bool *f=(bool*)malloc(n*sizeof(bool));
for(int j=0;j<n;j++)
*(f+j)=false;
*(b+i)=f;
}
}
bool issafe(int i,int j){
for(int k=0;k>=0 and k<n;k++){
if(b[k][j])
return false;
}
for(int k=0;k>=0 and k<n;k++){
if(b[i][k])
return false;
}
for(int k=i,l=j;k>=0 and l>=0;k--,l--){
if(b[k][l])
return false;
}
for(int k=i,l=j;k<n and l>=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;i++){
for(int j=0;j<n;j++){
if(b[i][j]){
v.push_back(j);

}

}
}
sol.push_back(v);
return true;
}
bool f=false;
for(int i=0;i<n;i++){

if(issafe(i,c)){
b[i][c]=1;
f=rec(r,c+1);
b[i][c]=0;
}
}
return f;
}
void soln(){
rec(0,0);
if(sol.size()){
sort(sol.begin(),sol.end());
cout<<"total solution "<<sol.size()<<endl;
for(int i=0;i<sol.size();i++){
for(int j=0;j<sol[i].size();j++)
cout<<sol[i][j]<<" ";
cout<<endl;
}
}
else
cout<<"NO OP";
}
};
int main(void){
int n;
cout<<"Enter number of queens ";
cin>>n;
nqueen q=nqueen(n);
q.soln();
}
78 changes: 78 additions & 0 deletions rat in a Maze find all possible solution.cpp
Original file line number Diff line number Diff line change
@@ -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 <bits/stdc++.h>
#define list vector<int>
using namespace std;


// } Driver Code Ends
// User function template for C++

class Solution{
public:
bool issafe(vector<list> &m, int i,int j,int n){
return (i<n && j<n && i>=0 &&j>=0 && m[i][j]==1 );
}
bool ss(vector<list> &m,int i,int j,int n,string s,vector<string>&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<string> findPath(vector<list > &m, int n) {
// Your code goes here
string s="";
vector<string> 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<list> m(n, vector<int> (n,0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> m[i][j];
}
}
Solution obj;
vector<string> 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