题库chevron_right图·搜索

图·搜索 模式

71 道题目

模式页适合用来建立可复用解题框架。先识别题目特征,再按固定流程解释状态定义、转移和边界。

识别信号

  • Do you understand how BFS guarantees the shortest path in a word transformation sequence?
  • Can you explain why a hash table improves the performance of this problem?
  • Do you understand how Depth-First Search can be applied to matrix traversal?

解题流程

  1. 1. 明确窗口/状态定义
  2. 2. 更新状态并维护约束
  3. 3. 用边界样例验证

常见失分点

  • Not checking if `endWord` is in the `wordList` before starting the BFS, leading to unnecessary computation.
  • Failing to properly mark border 'O's as safe, leading to incorrect regions being transformed.
  • Failing to mark visited cells, causing infinite loops or double-counting.

推荐题单梯度

#题目难度
127

单词接龙

Find the shortest transformation sequence from a start word to an end word, with each word in the sequence differing by …

困难
130

被围绕的区域

Transform the matrix in-place by marking regions surrounded by 'X' as 'X', while keeping border-adjacent 'O's intact.

中等
200

岛屿数量

Count the number of distinct islands in a binary grid using array traversal combined with depth-first search exploration…

中等
207

课程表

Determine if all courses can be completed by analyzing prerequisite dependencies using indegree tracking and topological…

中等
210

课程表 II

Solve the 'Course Schedule II' problem using graph indegree and topological ordering, utilizing DFS or BFS to find the c…

中等
211

添加与搜索单词 - 数据结构设计

Build a WordDictionary supporting dynamic word addition and search with wildcard matching efficiently using Trie and DFS…

中等
310

最小高度树

Identify all roots of a tree that produce minimum height using graph indegree analysis and topological trimming.

中等
365

水壶问题

Determine if two jugs with given capacities can measure an exact target amount using math and DFS strategies efficiently…

中等
386

字典序排数

Generate all numbers from 1 to n in lexicographical order using a depth-first search pattern optimized with trie logic f…

中等
407

接雨水 II

Solve Trapping Rain Water II using breadth-first search and priority queues for efficient water trapping in a matrix.

困难
417

太平洋大西洋水流问题

Find all cells on an island where water can flow to both the Pacific and Atlantic oceans using DFS or BFS.

中等
419

棋盘上的战舰

Count the number of non-overlapping battleships on a 2D board using array traversal and depth-first search patterns effi…

中等
433

最小基因变化

Determine the minimum number of single-character gene mutations to reach the target gene using BFS and a hash table look…

中等
463

岛屿的周长

Determine the perimeter of an island in a grid of land and water cells using DFS or BFS.

简单
529

扫雷游戏

Solve the Minesweeper game by updating revealed squares and handling clicks with Depth-First Search and Array manipulati…

中等
565

数组嵌套

Find the longest nested set in a permutation array using a depth-first traversal, handling cycles efficiently for medium…

中等
675

为高尔夫比赛砍树

Determine the minimum steps to cut all trees in a forest matrix in ascending height order using BFS traversal and priori…

困难
676

实现一个魔法字典

Design a Magic Dictionary to allow searching with one-character modifications, using Hash Table and String techniques.

中等
695

岛屿的最大面积

Find the largest connected land area in a binary grid using array traversal and depth-first search efficiently.

中等
733

图像渲染

Flood Fill is an array and DFS problem where you change connected pixels to a target color efficiently using recursion o…

简单
749

隔离病毒

Contain Virus involves using array-based Depth-First Search to contain viral spread by building walls around infected re…

困难
756

金字塔转换矩阵

The Pyramid Transition Matrix problem requires determining whether a pyramid can be formed with given blocks and valid p…

中等
802

找到最终的安全状态

Solve the problem of finding eventual safe states in a directed graph using depth-first search and topological sorting.

中等
827

最大人工岛

Calculate the largest island size by converting at most one zero in a binary grid using array and DFS techniques efficie…

困难
851

喧闹和富有

Determine the quietest person richer than each individual using graph indegree analysis and topological ordering techniq…

中等
854

相似度为 K 的字符串

K-Similar Strings involves finding the minimal number of swaps to transform one string into another through character sw…

困难
864

获取所有钥匙的最短路径

Find the minimum steps to collect all keys in a grid using BFS and bitmasking, handling locks efficiently.

困难
909

蛇梯棋

Solve the Snakes and Ladders problem using a BFS strategy to efficiently navigate the board and reach the final square.

中等
934

最短的桥

Find the minimum flips to connect two separate islands in a binary matrix using Array and DFS techniques efficiently.

中等
994

腐烂的橘子

Given a grid with fresh and rotten oranges, return the minimum time for all oranges to rot or -1 if impossible.

中等
1020

飞地的数量

This problem involves finding the number of land cells that cannot reach the boundary in a grid using DFS and array mani…

中等
1034

边界着色

Given a grid and a starting cell, color all border cells of the connected component using DFS while preserving interior …

中等
1091

二进制矩阵中的最短路径

Find the shortest clear path in a binary matrix using BFS, carefully handling obstacles and diagonal movements for effic…

中等
1129

颜色交替的最短路径

Solve the shortest path problem with alternating edge colors using graph traversal and BFS.

中等
1203

项目管理

Sort items into groups while respecting dependencies using graph indegree tracking and topological ordering patterns eff…

困难
1210

穿过迷宫的最少移动次数

Find the minimum moves for a 2-cell snake to reach the bottom-right corner using rotations and BFS traversal in a grid.

困难
1254

统计封闭岛屿的数目

Count the number of closed islands in a 2D grid using array traversal and depth-first search efficiently.

中等
1263

推箱子

Solve the minimum moves to push a box to its target using BFS and priority handling in a grid-based warehouse layout eff…

困难
1267

统计参与通信的服务器

Count Servers that Communicate involves identifying servers in a grid that can communicate based on row or column connec…

中等
1293

网格中的最短路径

Find the shortest path in a grid with obstacles, allowing the elimination of up to k obstacles using BFS.

困难
1298

你能从盒子里获得的最大糖果数

Collect maximum candies from boxes by exploring initially available boxes and using keys to unlock additional ones effic…

困难
1306

跳跃游戏 III

Jump Game III challenges you to determine if you can reach any zero in an array using jumps defined by array values, tes…

中等
1368

使网格图至少有一条有效路径的最小代价

Determine the minimum cost to create at least one valid path from the top-left to bottom-right in a directional grid.

困难
1391

检查网格中是否存在有效路径

Check if there is a valid path from the upper-left to the bottom-right corner of a grid using depth-first search.

中等
1462

课程表 IV

Determine if one course is a prerequisite of another using graph indegree tracking and topological ordering efficiently.

中等
1559

二维网格图中探测环

Detect cycles in a 2D character grid using DFS while carefully tracking parent cells to avoid invalid revisits.

中等
1568

使陆地分离的最少天数

Find the minimum number of days to disconnect an island in a grid using depth-first search.

困难
1625

执行操作后字典序最小的字符串

Optimize a string through rotations and additions to get the lexicographically smallest possible result using string man…

中等
1722

执行交换操作后的最小汉明距离

Solve Minimize Hamming Distance After Swap Operations by grouping swappable indices and matching value counts inside eac…

中等
1765

地图中的最高点

Solve the Map of Highest Peak problem using breadth-first search to assign heights based on proximity to water cells.

中等
1905

统计子岛屿

Identify and count all islands in grid2 that are fully contained within islands of grid1 using DFS traversal efficiently…

中等
1926

迷宫中离入口最近的出口

Find the shortest path from the maze entrance to the nearest border exit using BFS over a 2D array matrix efficiently.

中等
1992

找到所有的农场组

Identify all rectangular farmland groups in a binary matrix using array traversal and depth-first search efficiently.

中等
2039

网络空闲的时刻

Calculate the time when the network becomes idle, factoring in message resending and the BFS traversal method.

中等
2045

到达目的地的第二短时间

Find the second minimum time to reach a destination using BFS while accounting for traffic signal delays in a graph trav…

困难
2059

转化数字的最小运算数

Determine the minimum operations needed to convert a number using array elements with addition, subtraction, or XOR oper…

中等
2127

参加会议的最多员工数

Determine the maximum employees to invite based on favorite adjacency constraints using graph indegree and topological o…

困难
2146

价格范围内最高排名的 K 样物品

Use BFS to rank reachable shop items by distance, price, row, and column, then return the best k coordinates.

中等
2192

有向无环图中一个节点的所有祖先

Solve the All Ancestors of a Node in a Directed Acyclic Graph problem using graph traversal techniques like BFS, DFS, an…

中等
2246

相邻字符不同的最长路径

Find the longest path in a tree where adjacent nodes have different characters using graph DFS and topological reasoning…

困难
2290

到达角落需要移除障碍物的最小数目

Find the minimum obstacles to remove in a 2D grid to reach the bottom-right corner using BFS graph traversal techniques.

困难
2360

图中的最长环

The problem asks to find the longest cycle in a directed graph with specific edge constraints.

困难
2577

在网格图中访问一个格子的最少时间

Compute the fastest path in a grid where each cell has a minimum time requirement using BFS and priority queue technique…

困难
2596

检查骑士巡视方案

Validate a knight's movement configuration on an n x n chessboard to check if it forms a valid Knight's Tour.

中等
2608

图中的最短环

Find the shortest cycle in a bi-directional graph using BFS for optimal traversal.

困难
2612

最少翻转操作数

Find the minimum number of operations to move a 1 in an array from a given start position to other positions, considerin…

困难
2658

网格图中鱼的最大数目

Maximize the number of fish that can be caught by performing DFS on an optimal starting point in a grid.

中等
3015

按距离统计房屋对数目 I

Determine the number of house pairs at each street distance using graph traversal and breadth-first search efficiently.

中等
3243

新增道路查询后的最短距离 I

Solve shortest paths dynamically in a growing graph using BFS, updating distances efficiently after each road addition q…

中等
3286

穿越网格图的安全路径

Determine if you can safely traverse a binary grid from top-left to bottom-right using limited health points.

中等
3619

总价值可以被 K 整除的岛屿数目

Count the number of islands in a grid where the sum of each island's values is divisible by a given integer k.

中等

关联题型

LeetCode 图·搜索模式题解:71题训练路线