文章 212
评论 0
浏览 119893
DFS&BFS - 37. Sudoku Solver

DFS&BFS - 37. Sudoku Solver

Write a program to solve a Sudoku puzzle by filling the empty cells.

DFS - 980. Unique Paths III

DFS - 980. Unique Paths III

980. Unique Paths III On a 2-dimensional grid, there are 4 types of squares: 1 represents the starting square.  There is exactly one starting square. 2 represents the ending square.  There is exactly one ending square. 0 represents empty squares we can walk over. -1 represents obstacles that we cannot walk over. Return the number of 4-directional walks from the starting square to the ending square, that walk over every non-obstacle square ....

DFS&BFS - 52. N-Queens II

DFS&BFS - 52. N-Queens II

52. N-Queens II The n-queens puzzle is the problem of placing n queens on an n_×_n chessboard such that no two queens attack each other. Given an integer n, return the number of distinct solutions to the n-queens puzzle. Example: Input: 4 Output: 2 Explanation: There are two distinct solutions to the 4-queens puzzle as shown below. [  [".Q..",  // Solution 1   "...Q",   "Q...",   "..Q."],  ["..Q.",  // Solution 2 &n....

DFS&BFS - 51. N-Queens

DFS&BFS - 51. N-Queens

51. N-Queens The n-queens puzzle is the problem of placing n queens on an n_×_n chessboard such that no two queens attack each other. Given an integer n, return all distinct solutions to the n-queens puzzle. Each solution contains a distinct board configuration of the n-queens’ placement, where 'Q' and '.' both indicate a queen and an empty space respectively. Example: Input: 4 Output: [ [".Q..", // Solution 1 "...Q", "Q...", "..Q....

maze - dfs

maze - dfs

走迷宫可以用dfs或者dfs来做,当求最小路径的时候用bfs最方便。这里使用bfs来找迷宫的最短路径。做法就是先用bfs记录走到终点的过程中每一格的步数,这样从终点往回走就能走到最小路径。 输入的迷宫文件maze_file.in,0代表可以走的路: 6 5 0 1 0 0 0 0 0 0 1 0 0 1 1 1 0 1 1 0 0 0 0 1 0 1 1 0 1 0 0 0 代码: go: package main import ( "fmt" "os" ) type point struct { i, j int val int } func (p point) add(next point) point { return point{i: p.i + next.i, j: p.j + next.j} } // 判断点是否出界 func (p point) at(grid [][]int) (int, bool) { if p.i < 0 || p.i >= len(grid) || p.j < 0 || p.j >= len(grid[0]) { retu....

DFS&BFS - 130. Surrounded Regions

DFS&BFS - 130. Surrounded Regions

130. Surrounded Regions Given a 2D board containing 'X' and 'O' (the letter O), capture all regions surrounded by 'X'. A region is captured by flipping all 'O's into 'X's in that surrounded region. Example: X X X X X O O X X X O X X O X X After running your function, the board should be: X X X X X X X X X X X X X O X X Explanation: Surrounded regions shouldn’t be on the border, which means that any 'O' on the border of the board are not flipped....

DFS&BFS - 200. Number of Islands

DFS&BFS - 200. Number of Islands

200. Number of Islands Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You may assume all four edges of the grid are all surrounded by water. Example 1: Input: 11110 11010 11000 00000 Output: 1 Example 2: Input: 11000 11000 00100 00011 Output: 3 思路: 题目意思是在一个二维平面找出有多少个岛屿,可以使用dfs或者bfs做,这里使用dfs来做,每找到一个点是岛屿,就不断四个方向探索,把探索过的区域打上标记或者置为水域就可以。 代....

Nothing just happens, it's all part of a plan.