0% found this document useful (0 votes)
21 views8 pages

Backtracking Lecture 4

Uploaded by

Shivangi Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views8 pages

Backtracking Lecture 4

Uploaded by

Shivangi Rawat
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1.

Rat Maze With Multiple Jumps

class Solution
{
public int[][] ShortestDistance(int[][] mat) {
int n = mat.length;
int[][] v = new int[n][n];
boolean x = fun(mat, v, 0, 0, n);
if (x==false) {
return new int[][]{{-1}};
} else {
return v;
}
}

public boolean fun(int[][] mat, int[][] v, int i, int j, int n) {


if (i == n - 1 && j == n - 1) {
v[i][j] = 1;
return true;
}
if (i >= n || j >= n || i < 0 || j < 0 || mat[i][j] == 0) {
return false;
}

v[i][j] = 1;
for (int k = 1; k <= mat[i][j]; k++) {
if (fun(mat, v, i, j + k, n)) {
return true;
}
if (fun(mat, v, i + k, j, n)) {
return true;
}
}
v[i][j] = 0;

return false;
}
}
2. Find string in grid (flipkart, microsoft)

Logic is all chars have to be matched in one direction or you can say the character
matching in the rec call will be a on straight line and hence we fix the direction of
iteration in a particular dfs call and then after returning try out all the directions of
iteration , if any of the directions returns a true, we consider that cell as an answer,
break the dfs loop and continue on the next cell

class Solution {
public:
vector<vector<int>> d={{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}};
bool check(int x, int y,int i,vector<vector<char>>&grid,string &word, vector<int> &v)
{
if(i==word.length()) return true;
if(x<0 or y<0 or x>=grid.size() or y>=grid[0].size() or grid[x][y]!=word[i]) return false;
return check(x+v[0],y+v[1],i+1,grid,word,v);
}

vector<vector<int>>searchWord(vector<vector<char>>grid, string word){


vector<vector<int>> ans;
for(int i=0;i<grid.size();i++)
for(int j=0;j<grid[0].size();j++)
if(grid[i][j]==word[0])
for(auto &v:d)
if(check(i,j,0,grid,word,v))
{
ans.push_back({i,j});
break;
}
return ans;
}
};
3. Knight's Tour

#include <bits/stdc++.h>
using namespace std;

int dx[8]={2,2,-2,-2,1,1,-1,-1};
int dy[8]={1,-1,1,-1,2,-2,2,-2};

void rec(int x, int y, int n, vector<vector<int>>&res, int move)


{
if(y<0 or x<0 or x>=n or y>=n or res[x][y]>0)
{
return;
}

if(move==n*n)
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
cout<<res[i][j]<<" ";
}
cout<<endl;
}

cout<<endl;
return;
}

res[x][y]=move;

for(int k=0;k<8;k++)
{
int nx = x+ dx[k];
int ny= y+ dy[k];

rec(nx,ny,n,res,move+1);
}

res[x][y]=0;

}
int main()
{

int n=5;
int x=2,y=0;

vector<vector<int>>res(n,vector<int>(n,0));

int move=1;

rec(x,y,n,res,move);

return 0;
}
4. Generate Parentheses (Amazon, Google, Walmart, Adobe)

//User function Template for C++


// combinations of balanced parantheses
class Solution
{
public:

void rec(int open, int closed, string curr, vector<string>&ans)


{
if(open<0 or closed<0)
{
return;
}
if(open==0 && closed==0)
{
ans.push_back(curr);
return;
}

if(open>closed)
{
return;
}

rec(open-1,closed,curr+"(",ans);
rec(open,closed-1,curr+")",ans);
}
vector<string> AllParenthesis(int n)
{
// Your code goes here
vector<string>ans;
string curr="";
rec(n,n,curr,ans);
return ans;
}
};
5. N Digit numbers with digits in increasing order

//User function Template for C++


class Solution
{
public:
vector<int> v;

void store(int last,int n,int num)


{
if(n==0)
v.push_back(num);

for(int i=last+1;i<=9;i++)
{
int a = num*10 + i;
store(a%10,n-1,a);
}
}

vector<int> increasingNumbers(int N)
{
if(N==1){
store(-1,1,0);
}
else{
store(0,N,0);
}
return v;
}
};
6. N-Queen Problem

class Solution{
public:
bool isSafe(vector<vector<int>> board, int x, int y, int n){
for(int i = 0; i<x; i++){
if(board[i][y] == 1){
return false;
}
}

int row = x, col = y;


while(row >= 0 && col >= 0){
if(board[row][col] == 1){
return false;
}
row--;
col--;
}

row = x, col = y;
while(row >= 0 && col < n){
if(board[row][col] == 1){
return false;
}
row--;
col++;
}

return true;
}

void solve(int x, int n, vector<vector<int>> &board, vector<vector<int>> &result){


if(x == n){
vector<int> vt;
for(int i = 0; i<n; i++){
for(int j = 0; j<n; j++){
if(board[i][j] == 1){
vt.push_back(j+1);
}
}
}
result.push_back(vt);
return;
}

for(int col = 0; col < n; col++){


if(isSafe(board, x, col, n)){
board[x][col] = 1;
solve(x+1, n, board, result);
board[x][col] = 0;
}
}
}

vector<vector<int>> nQueen(int n) {
// code here
vector<vector<int>> result;
vector<vector<int>> board(n, vector<int>(n, 0));

solve(0, n, board, result);


sort(result.begin(), result.end());
return result;
}
};

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy