Skip to content

Commit

Permalink
Recursion types
Browse files Browse the repository at this point in the history
  • Loading branch information
Umang2002 authored Oct 6, 2020
1 parent 7f5ef42 commit a9f5b4c
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Recursion/headRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <string.h>

using namespace std;

void fun1(int n){

if(n > 0){

fun1(n-1);
cout << n <<endl;

}
}

int main(){
int x;
cin >> x;
fun1(x);
return 0;
}
23 changes: 23 additions & 0 deletions Recursion/indirectRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>
#include <string.h>

using namespace std;
void fun2(int);
void fun1(int n){
if(n > 0){
cout << n <<endl;
fun2(n-1);
}
}
void fun2(int n){
if(n > 1){
cout << n <<endl;
fun1(n/2);
}
}
int main(){
int x;
cin >> x;
fun1(x);
return 0;
}
59 changes: 59 additions & 0 deletions Recursion/knightproblem.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#include <iostream>

#include <iomanip>

using namespace std;

const int D =8;

bool canPlace(int board[D][D],int n,int r,int c){
return
r >= 0 && r <n &&
c >=0 && c < n &&
board[r][c]==0;
}
bool solveKnight(int board[D][D],int n,int move_no,int curRow,int curCol){
if(move_no == n*n){
return true;
}
int row_dir[]= {+2,+1,-1,-2,-2,-1,+1,+2};
int col_dir[]= {+1,+2,+2,+1,-1,-2,-2,-1};
for(int cur_dir =0;cur_dir<8;cur_dir++){
int nextRow = curRow + row_dir[cur_dir];
int nextCol = curCol + col_dir[cur_dir];
if(canPlace(board,n,nextRow,nextCol) == true){
board[nextRow][nextCol]= move_no + 1;
bool isSucess= solveKnight(board,n,move_no+1,nextRow,nextCol);
if(isSucess ==true){
return true;
}else {
board[nextRow][nextCol]=0;

}
}
}
return false;
}
void printBoard(int board[D][D],int n){
for(int i=0;i<n;i++){
for(int j=0;j <n;j++){
cout << setw(3) << board[i][j] << " ";
}
cout << endl;
}
}

int main(){
int board[D][D] = {0};
int n;
cin >>n;

board[0][0]=1;
bool ans = solveKnight(board,n,1,0,0);
if(ans == true ){

printBoard(board,n);
}else {
cout << "sorry!!!";
}
}
18 changes: 18 additions & 0 deletions Recursion/nestedRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <iostream>
#include <string.h>

using namespace std;
int fun1(int n){
if(n > 100){
return n-10;
}else{
return fun1(fun1(n+11));
}}
int main(){
int x;
int y;
cin >> x;
y =fun1(x);
cout << y <<endl;
return 0;
}
17 changes: 17 additions & 0 deletions Recursion/tailRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <string.h>

using namespace std;

void fun1(int n){

if(n > 0){
cout << n <<endl;
fun1(n-1);
}}
int main(){
int x;
cin >> x;
fun1(x);
return 0;
}
17 changes: 17 additions & 0 deletions Recursion/treeRecursion.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <iostream>
#include <string.h>

using namespace std;
void fun1(int n){
if(n > 0){
cout << n <<endl;
fun1(n-1);
fun1(n-1);
}
}
int main(){
int x;
cin >> x;
fun1(x);
return 0;
}

0 comments on commit a9f5b4c

Please sign in to comment.