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;
        }
    }
  • 相关阅读:
    Encryption (hard) CodeForces
    cf 1163D Mysterious Code (字符串, dp)
    AC日记——大整数的因子 openjudge 1.6 13
    AC日记——计算2的N次方 openjudge 1.6 12
    Ac日记——大整数减法 openjudge 1.6 11
    AC日记——大整数加法 openjudge 1.6 10
    AC日记——组合数问题 落谷 P2822 noip2016day2T1
    AC日记——向量点积计算 openjudge 1.6 09
    AC日记——石头剪刀布 openjudge 1.6 08
    AC日记——有趣的跳跃 openjudge 1.6 07
  • 原文地址:https://www.cnblogs.com/Allen-win/p/7581206.html
Copyright © 2011-2022 走看看