1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        ?LeetCode刷題實(shí)戰(zhàn)529:掃雷游戲

        共 1876字,需瀏覽 4分鐘

         ·

        2022-02-21 17:47

        算法的重要性,我就不多說(shuō)了吧,想去大廠,就必須要經(jīng)過(guò)基礎(chǔ)知識(shí)和業(yè)務(wù)邏輯面試+算法面試。所以,為了提高大家的算法能力,這個(gè)公眾號(hào)后續(xù)每天帶大家做一道算法題,題目就從LeetCode上面選 !

        今天和大家聊的問(wèn)題叫做?掃雷游戲,我們先來(lái)看題面:
        https://leetcode-cn.com/problems/minesweeper/



        示例? ? ? ? ? ? ? ? ? ? ? ? ?

        解題



        代碼如下:

        class?Solution?{
        public:
        ????int?dir_x[8] = {0, 1, 0, -1, 1, 1, -1, -1};
        ????int?dir_y[8] = {1, 0, -1, 0, 1, -1, 1, -1};
        ?
        ????void?dfs(vector<vector<char>>& board, int?x, int?y)?{
        ????????int?cnt = 0;
        ????????for?(int?i = 0; i < 8; ++i) {
        ????????????int?tx = x + dir_x[i];
        ????????????int?ty = y + dir_y[i];
        ????????????if?(tx < 0?|| tx >= board.size() || ty < 0?|| ty >= board[0].size()) {
        ????????????????continue;
        ????????????}
        ????????????// 不用判斷 M,因?yàn)槿绻?M 的話游戲已經(jīng)結(jié)束了
        ????????????cnt += board[tx][ty] == 'M';
        ????????}
        ????????if?(cnt > 0) {
        ????????????// 規(guī)則 3
        ????????????board[x][y] = cnt + '0';
        ????????} else?{
        ????????????// 規(guī)則 2
        ????????????board[x][y] = 'B';
        ????????????for?(int?i = 0; i < 8; ++i) {
        ????????????????int?tx = x + dir_x[i];
        ????????????????int?ty = y + dir_y[i];
        ????????????????// 這里不需要在存在 B 的時(shí)候繼續(xù)擴(kuò)展,因?yàn)?B 之前被點(diǎn)擊的時(shí)候已經(jīng)被擴(kuò)展過(guò)了
        ????????????????if?(tx < 0?|| tx >= board.size() || ty < 0?|| ty >= board[0].size() || board[tx][ty] != 'E') {
        ????????????????????continue;
        ????????????????}
        ????????????????dfs(board, tx, ty);
        ????????????}
        ????????}
        ????}
        ?
        ????vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        ????????int?x = click[0], y = click[1];
        ????????if?(board[x][y] == 'M') {
        ????????????// 規(guī)則 1
        ????????????board[x][y] = 'X';
        ????????} else?{
        ????????????dfs(board, x, y);
        ????????}
        ????????return?board;
        ????}
        };


        好了,今天的文章就到這里,如果覺得有所收獲,請(qǐng)順手點(diǎn)個(gè)在看或者轉(zhuǎn)發(fā)吧,你們的支持是我最大的動(dòng)力 。

        上期推文:

        LeetCode1-520題匯總,希望對(duì)你有點(diǎn)幫助!
        LeetCode刷題實(shí)戰(zhàn)521:最長(zhǎng)特殊序列 Ⅰ
        LeetCode刷題實(shí)戰(zhàn)522:最長(zhǎng)特殊序列 II
        LeetCode刷題實(shí)戰(zhàn)523:連續(xù)的子數(shù)組和
        LeetCode刷題實(shí)戰(zhàn)524:通過(guò)刪除字母匹配到字典里最長(zhǎng)單詞
        LeetCode刷題實(shí)戰(zhàn)525:連續(xù)數(shù)組
        LeetCode刷題實(shí)戰(zhàn)526:優(yōu)美的排列
        LeetCode刷題實(shí)戰(zhàn)527:?jiǎn)卧~縮寫
        LeetCode刷題實(shí)戰(zhàn)528:按權(quán)重隨機(jī)選擇

        瀏覽 54
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        評(píng)論
        圖片
        表情
        推薦
        點(diǎn)贊
        評(píng)論
        收藏
        分享

        手機(jī)掃一掃分享

        分享
        舉報(bào)
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            玩弄牲欲强老熟女tp121cc | 九九干 | 人人看人人搞 | 在健身房里啪啪嗯受不了了 | 色老板在线精品免费观看 | 日韩城人网站 | 男人的天堂色婷婷 | 欧美狠狠撸| 日本黄色影视 | 国模张雪馨私拍一二三在线 |