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

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=0 && i=0 && j0)
        {
            sum+=number%10;
            number/=10;
        }
        return sum;
    }
};

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

你可能感兴趣的:(C/C++,数据结构与算法,剑指Offer,JAVA)