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;
}
}