由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - 走迷宫的 时间复杂度是多少?谢谢
相关主题
有人面试碰到过scramble string这个题吗?一道面试算法题
贡献一道题boggle game是不是只有backtracking的解法?
Google第一轮面经写了一个Queens的backtrack 大牛帮我看看
帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...suduku solver这道题写代码有点难啊。
这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.来一题
找硬币的经典问题Google 电面面经
Apple Onsite?splunk面经,攒人品
求教一个题目,sudoku 下面代码哪里错了。。。请问:解 Sudoku 可以用什么算法?
相关话题的讨论汇总
话题: maze话题: int话题: sol话题: return
进入JobHunting版参与讨论
1 (共1页)
t**r
发帖数: 3428
1
走迷宫的 时间复杂度是多少?谢谢 如这个解法
#include
// Maze size
#define N 4
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N]);
/* A utility function to print solution matrix sol[N][N] */
void printSolution(int sol[N][N])
{
for (int i = 0; i < N; i++)
{
for (int j = 0; j < N; j++)
printf(" %d ", sol[i][j]);
printf("n");
}
}
/* A utility function to check if x,y is valid index for N*N maze */
bool isSafe(int maze[N][N], int x, int y)
{
// if (x,y outside maze) return false
if(x >= 0 && x < N && y >= 0 && y < N && maze[x][y] == 1)
return true;
return false;
}
/* This function solves the Maze problem using Backtracking. It mainly uses
solveMazeUtil() to solve the problem. It returns false if no path is
possible,
otherwise return true and prints the path in the form of 1s. Please note
that
there may be more than one solutions, this function prints one of the
feasible
solutions.*/
bool solveMaze(int maze[N][N])
{
int sol[N][N] = { {0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0},
{0, 0, 0, 0}
};
if(solveMazeUtil(maze, 0, 0, sol) == false)
{
printf("Solution doesn't exist");
return false;
}
printSolution(sol);
return true;
}
/* A recursive utility function to solve Maze problem */
bool solveMazeUtil(int maze[N][N], int x, int y, int sol[N][N])
{
// if (x,y is goal) return true
if(x == N-1 && y == N-1)
{
sol[x][y] = 1;
return true;
}
// Check if maze[x][y] is valid
if(isSafe(maze, x, y) == true)
{
// mark x,y as part of solution path
sol[x][y] = 1;
/* Move forward in x direction */
if (solveMazeUtil(maze, x+1, y, sol) == true)
return true;
/* If moving in x direction doesn't give solution then
Move down in y direction */
if (solveMazeUtil(maze, x, y+1, sol) == true)
return true;
/* If none of the above movements work then BACKTRACK:
unmark x,y as part of solution path */
sol[x][y] = 0;
return false;
}
return false;
}
// driver program to test above function
int main()
{
int maze[N][N] = { {1, 0, 0, 0},
{1, 1, 0, 1},
{0, 1, 0, 0},
{1, 1, 1, 1}
};
solveMaze(maze);
getchar();
return 0;
}
h*******e
发帖数: 1377
2
O(M*N)最大吧。。
t**r
发帖数: 3428
3
有人说是 o (m+n)
糊涂了

【在 h*******e 的大作中提到】
: O(M*N)最大吧。。
c*******y
发帖数: 98
4
Bfs最坏应该是m*n

【在 t**r 的大作中提到】
: 有人说是 o (m+n)
: 糊涂了

1 (共1页)
进入JobHunting版参与讨论
相关主题
请问:解 Sudoku 可以用什么算法?这种backtracking的问题怎么算时间复杂度?比如palindrom patitioning.
微软面试题一道找硬币的经典问题
问一题Apple Onsite?
0/1 Knapsack问题Linear Space的算法可以实现Backtrack吗?求教一个题目,sudoku 下面代码哪里错了。。。
有人面试碰到过scramble string这个题吗?一道面试算法题
贡献一道题boggle game是不是只有backtracking的解法?
Google第一轮面经写了一个Queens的backtrack 大牛帮我看看
帮俺看一下代码DP+DFS为什么过不了work break II 那个大case : aaaaaaa...suduku solver这道题写代码有点难啊。
相关话题的讨论汇总
话题: maze话题: int话题: sol话题: return