Skip to content

Commit 180faf0

Browse files
Merge branch 'smv1999:master' into integer-to-roman
2 parents 604b878 + 35892cb commit 180faf0

File tree

3 files changed

+245
-0
lines changed

3 files changed

+245
-0
lines changed

Backtracking/Knight's_Tour.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
'''
2+
Aim: To check whether it is possible for a Knight to visit each
3+
cell of the N*N chessboard without visiting any cell
4+
twice starting from (X, Y) position.
5+
Intution: To visit each and every positions which are available from
6+
the current position and recursively repeat this until
7+
all the cells are covered.
8+
9+
'''
10+
11+
from time import time
12+
13+
14+
def move_Knight(n, pos, move, ans, step):
15+
x, y = pos
16+
17+
# Base Case
18+
if step == n * n:
19+
return ans
20+
for i in move:
21+
r = x + i[0]
22+
c = y + i[1]
23+
if 0 <= r < n and 0 <= c < n and not ans[r][c]:
24+
ans[r][c] = step + 1
25+
temp = move_Knight(n, (r, c), move, ans, step + 1)
26+
if temp is not False:
27+
return temp
28+
ans[r][c] = 0
29+
return False
30+
31+
32+
def Knight_Tour(n, pos):
33+
x, y = pos
34+
35+
# All valid moves that a Knight can make
36+
move = [[2, 1], [2, -1], [-2, 1], [-2, -1],
37+
[1, 2], [1, -2], [-1, 2], [-1, -2]]
38+
39+
# To keep a track of already visited cells and
40+
# Answer Matrix
41+
answer = [[0]*n for i in range(n)]
42+
43+
# To mark (X, Y) cell as visited
44+
answer[x][y] = 1
45+
46+
return move_Knight(n, pos, move, answer, 1)
47+
48+
49+
# ------------------------DRIVER CODE ------------------------
50+
51+
if __name__ == "__main__":
52+
53+
# Input the initial Position of the Knight
54+
N = int(input("Enter the size of the Chessboard: "))
55+
X, Y = map(int, input("Enter Initial Position of the Knight: ").split())
56+
start = time()
57+
ans_mat = Knight_Tour(N, (X-1, Y-1))
58+
if ans_mat is False:
59+
print("Knight's Tour form the given initial position is not possible")
60+
else:
61+
print("The desired Knight's Tour :")
62+
for i in ans_mat:
63+
print("\t\t", *i)
64+
print("Time taken: ", time()-start)
65+
66+
'''
67+
68+
Sample Working:
69+
70+
Enter the size of the Chessboard: 5
71+
Enter the Initial Position of the Knight: 1 1
72+
The desired Knight's Tour :
73+
1 6 15 10 21
74+
14 9 20 5 16
75+
19 2 7 22 11
76+
8 13 24 17 4
77+
25 18 3 12 23
78+
Time taken: 0.10171318054199219
79+
80+
COMPLEXITY:
81+
82+
Time Complexity -> O(8^N)
83+
Space Complexity -> O(N^2)
84+
85+
'''
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Possible Words From Phone Digits
3+
Medium Accuracy: 51.47% Submissions: 11922 Points: 4
4+
Given a keypad as shown in the diagram, and an N digit number which is represented by array a[ ], the task is to list all words which are possible by pressing these numbers.
5+
6+
7+
Example 1:
8+
9+
Input: N = 3, a[] = {2, 3, 4}
10+
Output:
11+
adg adh adi aeg aeh aei afg afh afi
12+
bdg bdh bdi beg beh bei bfg bfh bfi
13+
cdg cdh cdi ceg ceh cei cfg cfh cfi
14+
Explanation: When we press 2,3,4 then
15+
adg, adh, adi, ... cfi are the list of
16+
possible words.
17+
Example 2:
18+
19+
Input: N = 3, a[] = {3, 4, 5}
20+
Output:
21+
dgj dgk dgl dhj dhk dhl dij dik dil
22+
egj egk egl ehj ehk ehl eij eik eil
23+
fgj fgk fgl fhj fhk fhl fij fik fil
24+
Explanation: When we press 3,4,5 then
25+
dgj, dgk, dgl, ... fil are the list of
26+
possible words.
27+
Your Task:
28+
You don't need to read input or print anything. You just need to complete the function possibleWords() that takes an array a[ ] and N as input parameters and returns an array of all the possible words in lexicographical increasing order.
29+
30+
Expected Time Complexity: O(4N * N).
31+
Expected Auxiliary Space: O(N).
32+
33+
Constraints:
34+
1 ≤ N ≤ 10
35+
2 ≤ a[i] ≤ 9
36+
*/
37+
38+
//Initial Template for C++
39+
40+
#include <bits/stdc++.h>
41+
#include <string>
42+
43+
using namespace std;
44+
45+
46+
// } Driver Code Ends
47+
//User function Template for C++
48+
49+
class Solution
50+
{
51+
public:
52+
//Function to find list of all words possible by pressing given numbers.
53+
54+
55+
void formstring(int a[], int N, string s, vector<string>&v, int i,map<int, vector<char>>m){
56+
if(i == N){
57+
v.push_back(s);
58+
return;
59+
}
60+
int pos = a[i];
61+
for(auto j : m[pos]){
62+
formstring(a,N,s+j,v,i+1,m);
63+
}
64+
}
65+
vector<string> possibleWords(int a[], int N)
66+
{
67+
map<int, vector<char>>m;
68+
int k = 0;
69+
for(int i = 2;i<=6;i++){
70+
int j = 0;
71+
while(j<3){
72+
m[i].push_back('a'+ j + k);
73+
j++;
74+
}
75+
k+=3;
76+
}
77+
m[7] = {'p','q','r','s'};
78+
m[8] = {'t','u','v'};
79+
m[9] = {'w','x','y','z'};
80+
81+
vector<string>ans;
82+
formstring(a,N,"",ans,0,m);
83+
return ans;
84+
}
85+
};
86+
87+
88+
// { Driver Code Starts.
89+
90+
int main() {
91+
92+
int T;
93+
94+
cin >> T; //testcases
95+
96+
while(T--){ //while testcases exist
97+
int N;
98+
99+
cin >> N; //input size of array
100+
101+
int a[N]; //declare the array
102+
103+
for(int i =0;i<N;i++){
104+
cin >> a[i]; //input the elements of array that are keys to be pressed
105+
}
106+
107+
Solution obj;
108+
109+
vector <string> res = obj.possibleWords(a,N);
110+
for (string i : res) cout << i << " ";
111+
cout << endl;
112+
}
113+
114+
return 0;
115+
} // } Driver Code Ends

Strings/string_permutations.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Problem: Print all permutations of string
2+
3+
// Input: ABC
4+
// Output: ABC ACB BAC BCA CBA CAB
5+
6+
// Concept: Backtracking
7+
8+
#include<bits/stdc++.h>
9+
#define fl(i,a,b) for(i=a;i<b;i++)
10+
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
11+
using namespace std;
12+
typedef long long int ll;
13+
vector<string> permutations;
14+
15+
vector<string> permute(string str, ll l, ll r)
16+
{
17+
if(l==r)
18+
{
19+
permutations.push_back(str);
20+
return permutations;
21+
}
22+
for(ll i=l; i<=r; i++)
23+
{
24+
swap(str[l], str[i]);
25+
permute(str, l+1, r);
26+
swap(str[l], str[i]);
27+
}
28+
return permutations;
29+
}
30+
31+
int main()
32+
{
33+
fast;
34+
35+
string str;
36+
cin>>str;
37+
38+
vector <string> ans= permute(str, 0, str.length()-1);
39+
40+
for(auto x: ans)
41+
cout<<x<<" ";
42+
43+
return 0;
44+
45+
}

0 commit comments

Comments
 (0)