zoukankan      html  css  js  c++  java
  • 机器人走方格问题

    package List;

    /**
    * Created by Administrator on 2015/10/10.
    */
    /*
    * 地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,
    * 每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。
    * 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),
    * 因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
    *
    * */
    public class demo2 {
    /**
    * sum是计算符合条件方格总数的方法
    * row是二维数组的行数
    * col是二维数组的列数
    * */
    public static int sum(int[][] grids,int row,int col,int threshold,int[] flag){
    if(numSum(row)+numSum(col)>threshold||row<0||col<0
    ||row>=grids.length||col>=grids[1].length||flag[row*(grids[1].length)+col]==1){
    return 0;
    }
    flag[row*(grids[1].length)+col]=1;
    return sum(grids,row-1,col,threshold,flag)
    +sum(grids,row+1,col,threshold,flag)
    +sum(grids,row,col-1,threshold,flag)
    +sum(grids,row,col+1,threshold,flag)+1;
    }
    public static int numSum(int i){
    int result=0;
    while (i%10!=0){
    result=result+i%10;
    i=i%10;
    }
    return result;
    }
    }
  • 相关阅读:
    1051 Wooden Sticks(贪心-3)
    97 等价交换(贪心-2)
    python文件操作
    python学习-day 2
    python学习-day 1
    Python 测试题目-1
    Python list和dict方法
    Python 字符串
    while循环语句
    Python if判断语句
  • 原文地址:https://www.cnblogs.com/hujingwei/p/4868085.html
Copyright © 2011-2022 走看看