机器人的运动范围

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

                                

例如我们输入的K=4,则机器人能够到达的格子数为:15。范围如上黄色区域所示。

算法分析:
        这个方格可以看成一个m*n的矩阵。同样在这个矩阵中,除边界上的格子之外其他格子都有四个相邻的格子。
  机器人从坐标(0,0)开始移动。当它准备进入坐标为(i,j)的格子时,通过检查坐标的数位和来判断机器人是否能够进入。如果机器人能够进入坐标为(i,j)的格子,我们接着再判断它能否进入四个相邻的格子(i,j-1)、(i-1,j),(i,j+1)和(i+1,j)。

/**************************************************************                     
* 题目描述:机器人的运动范围           
* 输入描述:请输入限制条件k: 
*           5 
*           请输入方格的行数m: 
*           10 
*           请输入方格的列数n: 
*           10 
* 程序输出: 矩阵能到达的方格数是: 
*           21 
***************************************************************/  
  
package org.marsguo.offerproject13;  
  
import java.util.Scanner;  
    /* 
    @param threshold    约束值 
    @param rows         方格行数 
    @param cols         方格列数 
    @return             最多可走的方格数 
            */  
class SolutionMethod1{  
    public int movingCount(int threshold,int rows,int cols){  
        boolean[] visted = new boolean[rows*cols];  //记录这个格子是否走过
        for(int i = 0; i < visted.length; i++)  
            visted[i] = false;  
          
        int count = movingCountCore(threshold,rows,cols,0,0,visted); //从(0,0)位置开始 
        return count;  
    }        
    /* 
    递归回溯方法: 
    @param threshold    约束值 
    @param rows         方格行数 
    @param cols         方格列数 
    @param row          当前处理的行号 
    @param col          当前处理的列号 
    @param visted       访问标记数组 
    @return             最多可走的方格 
    */  
    public int movingCountCore(int threshold,int rows,int cols,int row,int col,boolean[] visted){  
        int count = 0;  
        if(check(threshold,rows,cols,row,col,visted)){  
            visted[row*cols + col] = true;  //如果当前位置合法的话,标记这个格子走过
              
            count = 1 + movingCountCore(threshold,rows,cols,row - 1,col,visted) +  //上
                    movingCountCore(threshold,rows,cols,row,col - 1,visted) +   //左
                    movingCountCore(threshold,rows,cols,row + 1,col,visted) +   //下
                    movingCountCore(threshold,rows,cols,row,col + 1,visted);    //右
        }  
        return count;  
    }  
    //判断当前位置是否合法  
    boolean check(int threshold,int rows,int cols,int row,int col,boolean[] visted){  
        if(row >= 0 && row < rows && col >= 0 && col < cols  
                && (getDigitSum(row) + getDigitSum(col) <= threshold)  
                && !visted[row* cols + col])  
            return true;  
        return false;  
    }  
      
    /* 
    一个数字的位数之和 
    @param  number 数字 
    @return 数字的位数之和 
    */  
    public int getDigitSum(int number){  
        int sum = 0;  
        while(number > 0){  
            sum += number%10;  
            number /= 10;  
        }  
        return sum;  
    }  
}  
public class MovingCount {  
    public static void main(String[] args){  
        Scanner scanner = new Scanner(System.in);  
        System.out.println("请输入限制条件k:");  
        int k = scanner.nextInt();  
        System.out.println("请输入方格的行数m:");  
        int m = scanner.nextInt();  
        System.out.println("请输入方格的列数n:");  
        int n = scanner.nextInt();  
          
        SolutionMethod1 solution1 = new SolutionMethod1();  
          
        scanner.close();  
        System.out.println("矩阵能到达的方格数是:");  
        System.out.println(solution1.movingCount(k, m, n));  
    }  
}  

或者

public int movingCount(int threshold, int rows, int cols) {
    int flag[][] = new int[rows][cols]; //记录是否已经走过
    return helper(0, 0, rows, cols, flag, threshold);
}

private int helper(int i, int j, int rows, int cols, int[][] flag, int threshold) {
    if (i < 0 || i >= rows || j < 0 || j >= cols ||
            numSum(i) + numSum(j) > threshold || flag[i][j] == 1)
        return 0;
    flag[i][j] = 1;
    return helper(i - 1, j, rows, cols, flag, threshold)
            + helper(i + 1, j, rows, cols, flag, threshold)
            + helper(i, j - 1, rows, cols, flag, threshold)
            + helper(i, j + 1, rows, cols, flag, threshold) + 1;
}

private int numSum(int i) {
    int sum = 0;
    while (i > 0) {
        sum += i % 10;
        i = i / 10;
    }
    return sum;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值