Watch the rat try each path through the maze and back out of dead ends. Worked example on a 4 by 4 maze, then draw your own walls on a grid up to 10 by 10.
About this algorithm
Rat in a Maze asks for a route from the top-left cell of a grid to the bottom-right one, moving one cell at a time through open cells and never through walls. The backtracking solution walks forward as far as it can, and when the rat reaches a cell with no new way out, it steps back to the last cell that still has a direction left to try.
Use it when any route will do and you want the route itself as a list of cells. It is depth-first search on a grid with the current path kept on screen; the grid DFS page runs the same search with an explicit stack. Pick grid BFS instead when the route must be the shortest, because the first route this search finds can be much longer. Pick the variant that unmarks cells on the way back (see the note) when the task is to list every possible route, as some textbook versions of this puzzle ask.
The rat stands on cell (0, 0) and the cheese is on the bottom-right cell. The route so far is just the start.
From the current cell, try down, right, up and left, in that order. Skip a direction that leaves the grid, hits a wall, or reaches a cell already on the route or already known to lead nowhere.
Move to the first allowed neighbour, add it to the route and search again from there. Reaching the bottom-right cell ends the search, and the route is the answer.
If no direction is left, take the cell off the route, mark it as tried, and return to the cell before it, which goes on to its next direction. If the start cell runs out of directions, there is no route.
The page builds a random 6 by 6 maze each time (the slider runs from 4 to 10, and you can click cells to add or remove walls), so here is a small fixed one. Rows from the top: all four cells open; open, wall, wall, open; open, wall, open, open; wall, wall, open, open. Down comes first, so the rat goes to (1, 0) and then (2, 0). There, down and right are walls, up is where it came from and left is off the grid: a dead end. It backs up to (1, 0), which has nothing else to try, and then to (0, 0). Its next direction is right, so it walks (0, 1), (0, 2) and (0, 3), then down through (1, 3) and (2, 3) to the cheese at (3, 3). The route is 6 steps long, found after 8 moves forward and 2 steps back.
Many textbook versions unmark a cell when they back out of it, so a later route may pass through it again. That is needed to list every route, but it can take exponential time. This page marks a failed cell for good. That is ordinary depth-first search, and it still finds a route whenever one exists, because it reaches every open cell connected to the start.
Time: a cell on the route or marked as a dead end is never entered again, so each open cell is entered at most once and checks four directions. Space: the route and the recursion can stretch through every open cell in the worst case, and the marks take one entry per cell.