DEV Community

Ashutosh Sarangi
Ashutosh Sarangi

Posted on

1 1 1 1 1

Recursive Hard Problems Using Javascript

Q. How to find a way to Home / Source to destination using recurssion

"XXXXXEX",
"X X",

"XSXXXXX"

"XXXXXEX",
"X X X",
"X X X",
"X XXX X",
"X X",
"XSXXXXXX"

From these 2 patterns find out the path from Source to destination

// const maze = [
//     "XXXXXEX",
//     "X     X",
//     "XSXXXXX"
//     ];
const maze = [
    "XXXXXEX",
    "X   X X",
    "X   X X",
    "X XXX X",
    "X     X",
    "XSXXXXXX"
    ];
const dir = [
    [-1, 0],
    [1, 0],
    [0, -1],
    [0, 1]
    ];

visited = {};

path = [];

const findPath = (row, col) => {
    //1. out of bound condition
    if (row < 0 || row >= maze.length || col < 0 || col >= maze[0].length) {
        return false;
    }

    //2. Already visited 
    if (visited[`${row}_${col}`]) {
        return false;
    }


    // found road block 
    if (maze[row][col] === 'X') {
        return false;
    }

    // found the End

    if (maze[row][col] === 'E') {
         path.push(`${row}_${col}`);
        return true;
    }

    path.push(`${row}_${col}`);
    visited[`${row}_${col}`] = true;

    for (let item of dir) {
        const [x, y] = item;
        if (findPath(row+x, col+y)) {
            return true;
        }
    }
    path.pop();
    return false;
};

findPath(5,1);
// findPath(2,1);
console.log(path)

/*
node /tmp/n2GEk3kzOo.js
[
  '5_1', '4_1', '4_2',
  '4_3', '4_4', '4_5',
  '3_5', '2_5', '1_5',
  '0_5'
]
*/
Enter fullscreen mode Exit fullscreen mode

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (2)

Collapse
 
progkidscom profile image
ProgKids

Cool!

Collapse
 
ashutoshsarangi profile image
Ashutosh Sarangi

Thank you

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay