​LeetCode解法汇总2596. 检查骑士巡视方案

目录链接:

力扣编程题-解法汇总_分享+记录-CSDN博客

GitHub同步刷题项目:

https://github.com/September26/java-algorithms

原题链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台


描述:

骑士在一张 n x n 的棋盘上巡视。在有效的巡视方案中,骑士会从棋盘的 左上角 出发,并且访问棋盘上的每个格子 恰好一次 。

给你一个 n x n 的整数矩阵 grid ,由范围 [0, n * n - 1] 内的不同整数组成,其中 grid[row][col] 表示单元格 (row, col) 是骑士访问的第 grid[row][col] 个单元格。骑士的行动是从下标 0 开始的。

如果 grid 表示了骑士的有效巡视方案,返回 true;否则返回 false

注意,骑士行动时可以垂直移动两个格子且水平移动一个格子,或水平移动两个格子且垂直移动一个格子。下图展示了骑士从某个格子出发可能的八种行动路线。

示例 1:

输入:grid = [[0,11,16,5,20],[17,4,19,10,15],[12,1,8,21,6],[3,18,23,14,9],[24,13,2,7,22]]
输出:true
解释:grid 如上图所示,可以证明这是一个有效的巡视方案。

示例 2:


{
public:
    vector<vector<int>> forwards = {
        {2, 1},
        {2, -1},
        {1, 2},
        {1, -2},
        {-2, 1},
        {-2, -1},
        {-1, 2},
        {-1, -2}};
    pair<int, int> jumpStep(vector<vector<int>> &grid, int current, int x, int y)
    {

        for (vector<int> forward : forwards)
        {
            int newX = x + forward[0];
            int newY = y + forward[1];
            if (newX < 0 || newX >= grid.size() || newY < 0 || newY >= grid.size())
            {
                continue;
            }
            if (grid[newY][newX] != (current + 1))
            {
                continue;
            }
            return make_pair(newX, newY);
        }
        return make_pair(0, 0);
    }

    bool checkValidGrid(vector<vector<int>> &grid)
    {
        int i = 0;
        pair<int, int> postion = {0, 0};
        while (i < grid.size() * grid.size() - 1)
        {
            postion = jumpStep(grid, i, postion.first, postion.second);
            if (postion.first == 2 && postion.second == 3)
            {
                cout << "x" << endl;
            }
            if (postion.first == 0 && postion.second == 0)
            {
                return false;
            }
            i++;
        }
        return true;
    }
};
输入:grid = [[0,3,6],[5,8,1],[2,7,4]]
输出:false
解释:grid 如上图所示,考虑到骑士第 7 次行动后的位置,第 8 次行动是无效的。

提示:

  • n == grid.length == grid[i].length
  • 3 <= n <= 7
  • 0 <= grid[row][col] < n * n
  • grid 中的所有整数 互不相同

解题思路:

* 解题思路:

* 定义jumpStep方法,这个方法输入x,y坐标,以及当前是第几步,然后以当前坐标搜索周边8个点,如果有满足的则返回搜索到的坐标。

* 然后使用新坐标进行下一轮搜索,这时候步数+1,

代码:


{
public:
    vector<vector<int>> forwards = {
        {2, 1},
        {2, -1},
        {1, 2},
        {1, -2},
        {-2, 1},
        {-2, -1},
        {-1, 2},
        {-1, -2}};
    pair<int, int> jumpStep(vector<vector<int>> &grid, int current, int x, int y)
    {

        for (vector<int> forward : forwards)
        {
            int newX = x + forward[0];
            int newY = y + forward[1];
            if (newX < 0 || newX >= grid.size() || newY < 0 || newY >= grid.size())
            {
                continue;
            }
            if (grid[newY][newX] != (current + 1))
            {
                continue;
            }
            return make_pair(newX, newY);
        }
        return make_pair(0, 0);
    }

    bool checkValidGrid(vector<vector<int>> &grid)
    {
        int i = 0;
        pair<int, int> postion = {0, 0};
        while (i < grid.size() * grid.size() - 1)
        {
            postion = jumpStep(grid, i, postion.first, postion.second);
            if (postion.first == 2 && postion.second == 3)
            {
                cout << "x" << endl;
            }
            if (postion.first == 0 && postion.second == 0)
            {
                return false;
            }
            i++;
        }
        return true;
    }
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失落夏天

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值