From c7b69429c1218d9844b4bc9d46c0b83cd666c564 Mon Sep 17 00:00:00 2001 From: Nikhil Arora Date: Tue, 2 Oct 2018 01:05:40 +0530 Subject: [PATCH] added knight tour to backtracking and longest common string to dynamic --- Backtracking/knight-tour.cpp | 59 +++++++++++++++++ Dynamic Programming/longest_common_string.cpp | 65 +++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 Backtracking/knight-tour.cpp create mode 100644 Dynamic Programming/longest_common_string.cpp diff --git a/Backtracking/knight-tour.cpp b/Backtracking/knight-tour.cpp new file mode 100644 index 000000000..978a2d34b --- /dev/null +++ b/Backtracking/knight-tour.cpp @@ -0,0 +1,59 @@ +#include +# define n 8 +using namespace std; +bool issafe(int x,int y,int sol[n][n]) +{ + return (x=0 && y=0 && sol[x][y]==-1); + +} +bool solve(int x,int y, int mov, int sol[n][n], int xmov[n], int ymov[n]) +{ + int k,xnext,ynext; + + if(mov == n*n) + return true; + + for(k=0;k<8;k++) + { + xnext=x+xmov[k]; + ynext=y+ymov[k]; + + if(issafe(xnext,ynext,sol)) + { + sol[xnext][ynext]=mov; + + if(solve(xnext,ynext,mov+1,sol,xmov,ymov)==true) + return true; + else + sol[xnext][ynext]=-1; + } + } + return false; +} +int main() +{ + //initialize(); + + int sol[n][n]; + int i,j; + for(i=0;i +using namespace std; + +int max(int a,int b) +{ + return (a > b) ? a : b; +} + +int main() +{ + char str1[]="DEFBCD"; + char str2[]="ABDEFJ"; + int i,j,k; + int n=strlen(str1)+1; + int m=strlen(str2)+1; + //cout<ma) + { + ma=a[i][j]; + indi=i; + indj=j; + } + } + } + + cout<