Leecode 529. 扫雷游戏 DFS

news/2024/7/20 21:34:43 标签: 深度优先, 游戏, leetcode, c++, 算法

原题链接:Leecode 529. 扫雷游戏
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

class Solution {
public:
    vector<vector<int>> vis;
    int x[8]={-1,-1,-1,1,1,1,0,0};
    int y[8]={-1,0,1,-1,0,1,-1,1};
    int fun(vector<vector<char>>& board,int i,int j)
    {
        int m=board.size(),n=board[0].size();
        if(i<0 || i>m-1 || j<0 || j>n-1 ) return 0;
        if(board[i][j]=='M') return 1;
        return 0;
    }
    void dfs(vector<vector<char>>& board,int i,int j)
    {
        int m=board.size(),n=board[0].size();
        if(i<0 || i>m-1 || j<0 || j>n-1 || vis[i][j] ) return ;
        if(board[i][j]=='E')
        {
            vis[i][j]=1;
            int sum=0;
            for(int k=0;k<8;k++)
            {
                sum+=fun(board,i+x[k],j+y[k]);
            }
            if(sum) board[i][j]=sum+'0';
            else 
            {
                board[i][j]='B';
                for(int k=0;k<8;k++)
                {
                    dfs(board,i+x[k],j+y[k]);
                }
            }
        }
    }
    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        int m=board.size(),n=board[0].size();
        vis=vector<vector<int>>(m,vector<int>(n));
        int i=click[0],j=click[1];
        if(board[i][j]=='M')
        {
            board[i][j]='X';
            return board;
        }
        if(board[i][j]=='E')
        {
            dfs(board,i,j);
        }
        return board;
    }
};

http://www.niftyadmin.cn/n/1264727.html

相关文章

Leecode 133. 克隆图 DFS/BFS

原题链接&#xff1a;Leecode 133. 克隆图 官解&#xff1a;Leecode 133. 克隆图 DFS /* // Definition for a Node. class Node { public:int val;vector<Node*> neighbors;Node() {val 0;neighbors vector<Node*>();}Node(int _val) {val _val;neighbor…

df_1.columns

Python版本&#xff1a;Python 3.6 pandas.read_csv() 报错 OSError: Initializing from file failed&#xff0c;一般由两种情况引起&#xff1a;一种是函数参数为路径而非文件名称&#xff0c;另一种是函数参数带有中文&#xff08;包括路径里边有中文&#xff09;。 # -*- …

Leecode 1034. 边界着色 DFS/BFS

原题链接&#xff1a;Leecode 1034. 边界着色 这题题目不太好理解 DFS class Solution { public:int x[4]{-1,1,0,0};int y[4]{0,0,-1,1};vector<vector<int>> vis;bool dfs(vector<vector<int>>& grid,int i,int j,int col,int color){int mg…

在notebook中使用plt绘图共有三种模式

在notebook中使用plt绘图共有三种模式&#xff1a; %matplotlib inline&#xff1a;这是默认的模式&#xff0c;输出的图片是静态的%matplotlib auto&#xff1a;在这个模式下会弹出一个单独 的绘图窗口&#xff0c;和在pycharm中一样%matplotlib notebook&#xff1a;在这个模…

更换anaconda的镜像频道

1. 添加“清华镜像”渠道&#xff0c; 在Anaconda Prompt中执行&#xff1a; conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/ conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/ conda…

Leecode 417. 太平洋大西洋水流问题 DFS

原题链接&#xff1a;Leecode 417. 太平洋大西洋水流问题 从太平洋和大西洋各自遍历&#xff0c;重合的点即为答案。 class Solution { public:vector<vector<int>> P,A,res;void dfs(vector<vector<int>>& heights,vector<vector<int&g…

复杂网络中louvain算法实现时报错AttributeError: module ‘community‘ has no attribute ‘best_partition‘

导入包的方式有点奇怪&#xff0c;用的不是包名“python-louvain”而是“community”&#xff0c; import community as community_louvain 在jupyter中运行“partition community_louvain.best_partition(G) #进行图划分”的时候出现以下错误&#xff1a; AttributeError:…

ImportError: cannot import name ‘joblib‘

原因&#xff1a; 安装的Scikit-learn版本太高&#xff0c;我安装的版本是0.23.1 解决方法&#xff1a; 需要将Scikit-learn版本降到0.21以下 pip uninstall joblib scikit-learn sklearn pip install Scikit-learn0.20.4 或者直接安装joblib&#xff1a; pip install j…