#数据结构与算法学习笔记#剑指Offer64:机器人的运动范围 + 回溯法(Java、C/C++)

版权声明:本文为博主NJU_ChopinXBP原创文章,发表于CSDN,仅供交流学习使用,转载请私信或评论联系,未经博主允许不得转载。感谢您的评论与点赞。 https://blog.csdn.net/qq_20304723/article/details/88172646

2019.3.5     《剑指Offer》从零单刷个人笔记整理(66题全)目录传送门​​​​​​​

做了上一道回溯法的题目之后: #数据结构与算法学习笔记#剑指Offer63:矩阵中的路径 + 回溯法(Java、C/C++),发现套路居然如出一辙。

区别在于这道题不是是否存在特定路径的问题,而是最大可达连通区域的问题。思路有点区别,但是大体相似。建立一个全局的可达结点计数器result,一个记录是否已经访问的矩阵ispass,接下来递归遍历每个结点的前后左右,当某一结点已被访问、不可达或者不满足题目要求的时候,递归停止。


题目描述

地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?


Java实现:

/**
 * 
 * @author ChopinXBP
 * 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动
 * 每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。
 * 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。
 * 但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
 * 
 */

public class MovingCount_65 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(movingCount(3, 2, 2));
	}

	public static int result;
	
    public static int movingCount(int threshold, int rows, int cols)
    {
        if(threshold < 0 || rows <= 0 || cols <= 0) return 0;
        result = 0;
        boolean[][] ispass = new boolean[rows][cols]; 
        Solution(0, 0, threshold, rows, cols, ispass);
        return result;
    }
    
    public static void Solution(int x, int y, int threshold, int rows, int cols, boolean[][] ispass) {
    	if(x < 0 || x >= rows || y < 0 || y >= cols || ispass[x][y])return;
    	if(threshold < Sum(x, y)) return;
    	result++;
    	ispass[x][y] = true;
    	Solution(x - 1, y, threshold, rows, cols, ispass);
    	Solution(x + 1, y, threshold, rows, cols, ispass);
    	Solution(x, y - 1, threshold, rows, cols, ispass);
    	Solution(x, y + 1, threshold, rows, cols, ispass);
    }
    
    public static int Sum(int x, int y) {
    	int result = 0;
    	while(x != 0) {
    		result += x % 10;
    		x /= 10;
    	}
    	while(y != 0) {
    		result += y % 10;
    		y /= 10;
    	}
    	return result;
    }
}

C++实现示例:

class Solution {
public:
    int movingCount(int threshold, int rows, int cols)
    {
        bool* flag=new bool[rows*cols];
        for(int i=0;i<rows*cols;i++)
            flag[i]=false;
        int count=moving(threshold,rows,cols,0,0,flag);//从(0,0)坐标开始访问;
        delete[] flag;
        return count;
    }
    //计算最大移动位置
    int moving(int threshold,int rows,int cols,int i,int j,bool* flag)
    {
        int count=0;
        if(check(threshold,rows,cols,i,j,flag))
        {
            flag[i*cols+j]=true;
            //标记访问过,这个标志flag不需要回溯,因为只要被访问过即可。
           //因为如果能访问,访问过会加1.不能访问,也会标记下访问过。
            count=1+moving(threshold,rows,cols,i-1,j,flag)
                   +moving(threshold,rows,cols,i,j-1,flag)
                   +moving(threshold,rows,cols,i+1,j,flag)
                   +moving(threshold,rows,cols,i,j+1,flag);
        }
        return count;
    }
    //检查当前位置是否可以访问
    bool check(int threshold,int rows,int cols,int i,int j,bool* flag)
    {
        if(i>=0 && i<rows && j>=0 && j<cols
            && getSum(i)+getSum(j)<=threshold
            && flag[i*cols+j]==false)
           return true;
        return false;
    }
    //计算位置的数值
    int getSum(int number)
    {
        int sum=0;
        while(number>0)
        {
            sum+=number%10;
            number/=10;
        }
        return sum;
    }
};

#Coding一小时,Copying一秒钟。留个言点个赞呗,谢谢你#

猜你喜欢

转载自blog.csdn.net/qq_20304723/article/details/88172646