Backtracking algorithm: rat in maze

Sept. 10, 2015

Study again the back tracking algorithm using recursive solution, rat in maze, a classical problem. Made a few of mistakes through the practice, one is how to use two dimension array, another one is that "not all return path returns value", not so confident that "return false" at the end of function.

重温二年前做过的算法题, Rat in Maze, 为自己惭愧! 二年前的练习, 没有任何的参考网页信息, 也没有算法讨论, 尝试改进. 感觉到自己的差距, 这次练习, 着重强调把算法能背出来. 一步一步写出来, 发现几个错误. 二维数组不熟悉, 耽误了十几分钟; 另外, 就是, "return false" 在递归函数最后一句, 先是忘了. 总之, 这个经典题目, 十分钟写不出来; 需要二个数组, 边界条件检测, 一点印象没有.

Here are my favorite blogs about this problem:

1. http://www.geeksforgeeks.org/backttracking-set-2-rat-in-a-maze/

2. http://algorithms.tutorialhorizon.com/backtracking-rat-in-a-maze-puzzle/

3. https://www.cs.bu.edu/teaching/alg/maze/

Julia‘s C# pratice:

https://github.com/jianminchen/AlgorithmsPractice/blob/master/RatInAMaze_BackTracking.cs

And then, try to find more discussion about this problem, came cross blogs to challenge my analysis skills. 搜一下Google, 找到一个很有深度的网页, 看了以后, 体验一下代码, 感觉比自己的水平高了几个档次.

http://blogs.msdn.com/b/mattwar/archive/2005/02/03/366498.aspx

http://blogs.msdn.com/b/mattwar/archive/2005/02/11/371274.aspx

More code to read and then play:

http://www.evercrest.com/ext/CheeseAppropriator.cs

and C# practice:

https://github.com/jianminchen/AlgorithmsPractice/blob/master/MousingAround.cs

时间: 2024-10-12 02:08:13

Backtracking algorithm: rat in maze的相关文章

[email protected] Check whether a given graph is Bipartite or not

Check whether a given graph is Bipartite or not A Bipartite Graph is a graph whose vertices can be divided into two independent sets, U and V such that every edge (u, v) either connects a vertex from U to V or a vertex from V to U. In other words, fo

Uva 10401 Injured Queen Problem ( 计数DP)

Problem I Injured Queen Problem Input: standard input Output: standard output Time Limit: 6 seconds Memory Limit: 32 MB Chess is a two-player board game believed to have been played in India as early as the sixth century. However, in this problem we

HDU 1010 Tempter of the Bone_DFS

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 char maze[8][8]; 7 int n, m, t; 8 int mx, my, X, Y; 9 int flag, tt; 10 int Move[4][2]= {{-1,0},{1,0},{0,1},{0,-1}}; 11

Jeff Somers&#39;s N Queens Solutions 最快的n皇后算法

1 /* Jeff Somers 2 * 3 * Copyright (c) 2002 4 * 5 * [email protected] 6 * or 7 * [email protected] 8 * 9 * April, 2002 10 * 11 * Program: nq 12 * 13 * Program to find number of solutions to the N queens problem. 14 * This program assumes a twos compl

【数据结构】10分钟教你用栈求解迷宫老鼠问题超详细教程附C++源代码

问题描述 给定一张迷宫地图和一个迷宫入口,然后进入迷宫探索找到一个出口.如下图所示: 该图是一个矩形区域,有一个入口和出口.迷宫内部包含不能穿越的墙壁或者障碍物.这些障碍物沿着行和列放置,与迷宫的边界平行.迷宫的入口在左上角,出口在右下角. 问题分析 首先要有一张迷宫地图,地图由两部分组成: (1)一是迷宫中各处的位置坐标, (2)二是迷宫各位置处的状态信息,即该处是墙还是路 所以,该迷宫地图可由一个二维数组来表示.数组的横纵坐标表示迷宫各处的位置坐标,数组元素表示各位置处的状态信息. 2.在这

DFS、BFS和Backtracking模板

区别与联系 区别 DFS多用于连通性问题因为其运行思想与人脑的思维很相似,故解决连通性问题更自然,采用递归,编写简便(但我个人不这样觉得...) DFS的常数时间开销会较少.所以对于一些能用DFS就能轻松解决的,为何要用BFS? 一般来说,能用DFS解决的问题,都能用BFS. BFS多用于解决最短路问题,其运行过程中需要储存每一层的信息,所以其运行时需要储存的信息量较大,如果人脑也可储存大量信息的话,理论上人脑也可运行BFS. Backtracking相当于在DFS的基础上进行剪枝. 联系 BF

hdu 5094 Maze bfs+状态压缩

Maze Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others) Total Submission(s): 642    Accepted Submission(s): 229 Problem Description This story happened on the background of Star Trek. Spock, the deputy captain of St

Fire Maze(广度优先搜索)

Fire Maze Time Limit: 1000 MS Memory Limit: 32768 K Total Submit: 53(19 users) Total Accepted: 26(17 users) Rating: Special Judge: No Description After escaping from Figo's chase, Severus falls into a N * M maze designed by Figo. At first, Severus is

Uva 705 - Slash Maze

  Slash Maze  By filling a rectangle with slashes (/) and backslashes ( ), you can generate nice little mazes. Here is an example: As you can see, paths in the maze cannot branch, so the whole maze only contains cyclic paths and paths entering somewh