文章目录
- 太平洋大西洋流水问题
- 扫雷游戏
- 迷路的机器人
太平洋大西洋流水问题
class Solution {
public:vector<vector<int>> res;int m = 0, n = 0;vector<vector<int>> pacificAtlantic(vector<vector<int>>& heights) {m = heights.size();n = heights[0].size();vector<vector<bool>> check1(m, vector<bool>(n, false));vector<vector<bool>> check2(m, vector<bool>(n, false));for (int i = 0; i < m; ++i) {if (!check1[i][0])dfs(heights, check1, i, 0);if (!check2[i][n - 1])dfs(heights, check2, i, n - 1);}for (int i = 0; i < n; ++i) {if (!check1[0][i])dfs(heights, check1, 0, i);if (!check2[m - 1][i])dfs(heights, check2, m - 1, i);}for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j) {if (check1[i][j] && check2[i][j])res.push_back({i, j});}return res;}void dfs(vector<vector<int>>& heights, vector<vector<bool>>& check, int i, int j) {check[i][j] = true;int dx[4] = {0,0,1,-1};int dy[4] = {1,-1,0,0};for(int k = 0; k < 4; k++){int x = i + dx[k];int y = j + dy[k];if(x >= 0 && y >= 0 && x < m && y < n && heights[x][y] >= heights[i][j] && !check[x][y])dfs(heights, check, x, y);}}
};
扫雷游戏
代码1 与 代码2 的相似度达到 99%,但其差异性在逻辑思维上存在先后影响。有兴趣的道友可以细细体会一下
代码1:
class Solution {
public:int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};bool check[51][51] = {false};int m = 0, n = 0;vector<vector<char>> updateBoard(vector<vector<char>>& board,vector<int>& click) {// 预处理,将已经处理过的点标记m = board.size();n = board[0].size();for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j) {if (board[i][j] != 'M' && board[i][j] != 'E')check[i][j] = true;}// 解题默认点击未挖出的点:未挖出的点分为两种情况:M & Eif (board[click[0]][click[1]] == 'M') {board[click[0]][click[1]] = 'X';} else if (board[click[0]][click[1]] == 'E') {dfs(board, click[0], click[1]);}return board;}void dfs(vector<vector<char>>& board, int i, int j) {board[i][j] = 'B';int count = 0;for (int k = 0; k < 8; ++k) {int x = i + dx[k];int y = j + dy[k];if (x >= 0 && y >= 0 && x < m && y < n && board[x][y] == 'M')count++;}if (count != 0)board[i][j] = '0' + count;else {for (int k = 0; k < 8; ++k) {int x = i + dx[k];int y = j + dy[k];if (x >= 0 && y >= 0 && x < m && y < n && !check[x][y] && board[x][y] == 'E')dfs(board, x, y);}}// 处理过的点进行标记check[i][j] = true;}
};
代码2:
class Solution {
public:int dx[8] = {-1, -1, -1, 0, 0, 1, 1, 1};int dy[8] = {-1, 0, 1, -1, 1, -1, 0, 1};bool check[51][51] = {false};int m = 0, n = 0;vector<vector<char>> updateBoard(vector<vector<char>>& board,vector<int>& click) {// 预处理,将已经处理过的点标记m = board.size();n = board[0].size();for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j) {if (board[i][j] != 'M' && board[i][j] != 'E')check[i][j] = true;}// 解题默认点击未挖出的点:未挖出的点分为两种情况:M & Eif (board[click[0]][click[1]] == 'M') {board[click[0]][click[1]] = 'X';} else if (board[click[0]][click[1]] == 'E') {dfs(board, click[0], click[1]);}return board;}void dfs(vector<vector<char>>& board, int i, int j) {board[i][j] = 'B';int count = 0;for (int k = 0; k < 8; ++k) {int x = i + dx[k];int y = j + dy[k];if (x >= 0 && y >= 0 && x < m && y < n && board[x][y] == 'M')count++;}// 处理过的点进行标记check[i][j] = true;if (count != 0)board[i][j] = '0' + count;else {for (int k = 0; k < 8; ++k) {int x = i + dx[k];int y = j + dy[k];if (x >= 0 && y >= 0 && x < m && y < n && !check[x][y])dfs(board, x, y);}}}
};
迷路的机器人
class Solution {
public:vector<vector<int>> res;int m = 0, n = 0;bool check[101][101] = {false};vector<vector<int>> pathWithObstacles(vector<vector<int>>& obst) {m = obst.size();n = obst[0].size();if (obst[0][0] == 1 || obst[m - 1][n - 1] == 1)return {};res.push_back({0, 0});for (int i = 0; i < m; ++i)for (int j = 0; j < n; ++j) {if (obst[i][j] == 1)check[i][j] = true;}if (!dfs(obst, 0, 0))res.pop_back();return res;}bool dfs(vector<vector<int>>& obst, int i, int j) {if (res.size() == m + n - 1) {return true;}int dx[2] = {0, 1};int dy[2] = {1, 0};for (int k = 0; k < 2; k++) {int x = i + dx[k];int y = j + dy[k];if (x < m && y < n && !check[x][y]) {if (obst[x][y] == 0) {res.push_back({x, y});if (dfs(obst, x, y))return true;else {res.pop_back();check[x][y] = true;}}}}return false;}
};