LeetCode周赛#103 Q2 Snakes and Ladders (BFS)

题目来源:https://leetcode.com/contest/weekly-contest-103/problems/snakes-and-ladders/

问题描述

909. Snakes and Ladders

On an N x N board, the numbers from 1 to N*N are written boustrophedonically starting from the bottom left of the board, and alternating direction each row.  For example, for a 6 x 6 board, the numbers are written as follows:

You start on square 1 of the board (which is always in the last row and first column).  Each move, starting from square x, consists of the following:

  • You choose a destination square S with number x+1x+2x+3x+4x+5, or x+6, provided this number is <= N*N.
    • (This choice simulates the result of a standard 6-sided die roll: ie., there are always at most 6 destinations.)
  • If S has a snake or ladder, you move to the destination of that snake or ladder.  Otherwise, you move to S.

A board square on row r and column c has a "snake or ladder" if board[r][c] != -1.  The destination of that snake or ladder is board[r][c].

Note that you only take a snake or ladder at most once per move: if the destination to a snake or ladder is the start of another snake or ladder, you do not continue moving.  (For example, if the board is `[[4,-1],[-1,3]]`, and on the first move your destination square is `2`, then you finish your first move at `3`, because you do not continue moving to `4`.)

Return the least number of moves required to reach square N*N.  If it is not possible, return -1.

Example 1:

Input: [
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,-1,-1,-1,-1,-1],
[-1,35,-1,-1,13,-1],
[-1,-1,-1,-1,-1,-1],
[-1,15,-1,-1,-1,-1]]
Output: 4
Explanation: 
At the beginning, you start at square 1 [at row 5, column 0].
You decide to move to square 2, and must take the ladder to square 15.
You then decide to move to square 17 (row 3, column 5), and must take the snake to square 13.
You then decide to move to square 14, and must take the ladder to square 35.
You then decide to move to square 36, ending the game.
It can be shown that you need at least 4 moves to reach the N*N-th square, so the answer is 4.

Note:

  1. 2 <= board.length = board[0].length <= 20
  2. board[i][j] is between 1 and N*N or is equal to -1.
  3. The board square with number 1 has no snake or ladder.
  4. The board square with number N*N has no snake or ladder.

------------------------------------------------------------

题意

飞行棋游戏,如果棋盘上有正整数,则可以飞到那个正整数所在的格子,否则只能掷骰子向前选择走1/2/3/4/5/6步。注意,不能连续飞行,即飞到的格子上也有一个正整数,但这时候不能再飞了,只能掷骰子向前走。求从第1个格子到第N*N个格子的最小掷骰子次数。

------------------------------------------------------------

思路

看到大神用Dijkstra算法解的,根据输入棋盘上的数和移动规则计算出邻接矩阵,然后把问题转化为最短路,用堆优化的Dijkstra算法解,复杂度为O(NlogN),因为这题是稀疏路,m∝N.

笔者在做题的时候没有想到这个方法,是用BFS做的,复杂度是O(N^2)。用BFS做的要点是由于不能连续飞行的限制,因此BFS中记录走到每个格子需要的最小步数的vis数组要设置两个: vis1/vis0,vis1表示到达该格子的最后一步使用飞行的步数,vis0表示到达该格子的最后一步未使用飞行的步数。

------------------------------------------------------------

代码

class Solution {
public:
    struct node{
        int num, hop;
        bool jump;

        node (void): num(1), hop(0), jump(0) {}
        node (int nn, int hh, bool jj): num(nn), hop(hh), jump(jj) {}
    };

    int ind[2];
    int vis0[500], vis1[500];

    void cal_ind(int num, int N)
    {
        ind[0] = N - 1 - (num-1) / N;
        if ((N - 1 - ind[0]) % 2 == 0)
        {
            ind[1] = num - (N - 1 - ind[0]) * N - 1;
        }
        else
        {
            ind[1] = N - num + (N - 1 - ind[0]) * N;
        }
    }

    int bfs(vector<vector<int> >& board)
    {
        memset(vis0, 0x3f, sizeof(vis0));
        memset(vis1, 0x3f, sizeof(vis1));
        int N = board[0].size(), num, hops, ans = 500;
        bool jump = false;
        node nd;
        queue<node> q;
        q.push(node(1,0, 0));
        vis0[1] = 0;
        while (!q.empty())
        {
            nd = q.front();
            q.pop();
            if (nd.num == N*N && nd.hop < ans)
            {
                ans = nd.hop;
            }
            cal_ind(nd.num, N);
            num = nd.num;
            hops = nd.hop;
            jump = nd.jump;
//          cout << num << "\t" << hops << endl;
            if (board[ind[0]][ind[1]] == -1 || jump)
            {
                if (num+1 <= N*N && hops+1 < vis0[num+1])
                {
                    q.push(node(num+1, hops+1, 0));
                    vis0[num+1] = hops+1;
                }
                if (num+2 <= N*N && hops+1 < vis0[num+2])
                {
                    q.push(node(num+2, hops+1, 0));
                    vis0[num+2] = hops+1;
                }
                if (num+3 <= N*N && hops+1 < vis0[num+3])
                {
                    q.push(node(num+3, hops+1, 0));
                    vis0[num+3] = hops+1;
                }
                if (num+4 <= N*N && hops+1 < vis0[num+4])
                {
                    q.push(node(num+4, hops+1, 0));
                    vis0[num+4] = hops+1;
                }
                if (num+5 <= N*N && hops+1 < vis0[num+5])
                {
                    q.push(node(num+5, hops+1, 0));
                    vis0[num+5] = hops+1;
                }
                if (num+6 <= N*N && hops+1 < vis0[num+6])
                {
                    q.push(node(num+6, hops+1, 0));
                    vis0[num+6] = hops+1;
                }
            }
            else
            {
                if (board[ind[0]][ind[1]] != -1 && board[ind[0]][ind[1]] != 1 &&
                       (vis1[board[ind[0]][ind[1]]] > hops))
                {
                    q.push(node(board[ind[0]][ind[1]], hops, 1));
                    vis1[board[ind[0]][ind[1]]] = hops;
                }
            }
        }
        return ans == 500 ? -1 : ans;
    }
    int snakesAndLadders(vector<vector<int> >& board) {
        return bfs(board);
    }
};

猜你喜欢

转载自blog.csdn.net/da_kao_la/article/details/82827778
Q2