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

    package edu.bjtu.day8_27;
    
    import java.util.Scanner;
    
    /**
     * @author Allen
     * @version 创建时间:2017年8月27日 下午7:55:46
     * 类说明:链接:https://www.nowcoder.com/questionTerminal/6e5207314b5241fb83f2329e89fdecc8
    地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,
    但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),
    因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子? 
     */
    
    public class MainNumOfPoint {
        public static void main(String args[]){
            Soution solution = new Soution();
            Scanner sc=new Scanner(System.in);
            String str=sc.nextLine();
            String[] strArr=str.split(",");
            int a=Integer.parseInt(strArr[0]);
            int b=Integer.parseInt(strArr[1]);
            int c=Integer.parseInt(strArr[2]);
            
            int num=solution.moveingCount(a,b,c);
            System.out.println(num);
        }
    }
    
    class Soution{
        public int moveingCount(int key,int rows,int cols){
            boolean flag[][]=new boolean[rows][cols];
            return helper(flag,rows,cols,key,0,0);
        }
        
        int helper(boolean flag[][],int rows, int cols, int threshold,
                int initRow,int initCol){
            
            if(initRow < 0 || initRow >=rows || initCol <0 || initCol >= cols ||
                    bitsum(initRow)+bitsum(initCol)>threshold || flag[initRow][initCol]){
                return 0;
            }
                    
            flag[initRow][initCol]=true;
            
        /*    for(int i=0; i<rows; i++){
                for(int j=0; j<cols; j++){
                    System.out.print(flag[i][j]);
                }
                System.out.println();
            }
            System.out.println();*/
            
            return helper(flag, rows, cols, threshold, initRow-1, initCol)+
                    helper(flag, rows, cols, threshold, initRow+1, initCol)+
                    helper(flag, rows, cols, threshold, initRow, initCol-1)+
                    helper(flag, rows, cols, threshold, initRow, initCol+1)+1;                
            }
        
        int bitsum(int num){
            int sum=0;
            while(num!=0){
            sum+=num%10;
            num/=10;
            }
            return sum;
        }
    }
  • 相关阅读:
    Apache 阿帕奇 配置运行环境
    2019年6月多校联训b层——搜索算法 Problem A 宽搜 营救
    西安集训B Day1 test 问题 C: 小明的城堡
    西安集训B层Day1 test 问题 A: 旅行日记
    二分答案—洛谷P1182 数列分段`Section II`
    2019.5.25 Noip模拟测试2 T2题解
    2019.5.25 Noip模拟测试2 T1题解
    DP专题练习 toasting
    2019.5.1 DP专题训练 山峰数(hill)
    React 点击按钮显示div与隐藏div
  • 原文地址:https://www.cnblogs.com/Allen-win/p/7581206.html
Copyright © 2011-2022 走看看