题目:
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
思路:
利用递归回溯的方法,写一个sum()方法,求i和j的各个位相加的和,并在移动时判断sum是否大于threshold
Java
package nowcoder;
public class S66_MovingCount {
public int movindCount(int threshold, int rows, int cols){
int[][] flag = new int[rows][cols];
return moving(threshold, flag, rows, cols, 0, 0);
}
public int moving(int threshold, int[][] flag, int rows, int cols, int i, int j){
if (i>=rows || j >= cols || i<0 || j<0 || threshold <0 || sum(i, j) > threshold || flag[i][j] ==1)
return 0;
flag[i][j] = 1;
return moving(threshold, flag, rows, cols, i-1, j)+
moving(threshold, flag, rows, cols, i+1, j)+
moving(threshold, flag, rows, cols, i, j-1)+
moving(threshold, flag, rows, cols, i, j+1)+1;
}
public int sum(int i, int j){
if (i < 0 || j <0)
return 0;
int sum = 0;
while (i>0 && j>0){
sum += i%10;
i = i/10;
sum += j%10;
j = j/10;
}
return sum;
}
public static void main(String[] args){
S66_MovingCount s66 = new S66_MovingCount();
System.out.println(s66.movindCount(2, 4, 4));
}
}
Python
class MovingCount:
def movingCount(self, threshold, rows, cols):
flag = [[0] * cols for i in range(rows)]
return self.moving(threshold, rows, cols, 0, 0, flag)
def moving(self, threshold, rows, cols, i, j, flag):
if i >= rows or j >= cols or i < 0 or j < 0 or self.sum(i, j) > threshold or flag[i][j] == 1:
return 0
flag[i][j] = 1
return self.moving(threshold, rows, cols, i-1, j, flag) + \
self.moving(threshold, rows, cols, i+1, j, flag) + \
self.moving(threshold, rows, cols, i, j-1, flag) + \
self.moving(threshold, rows, cols, i, j+1, flag) + 1
def sum(self, i, j):
if i < 0 or j < 0:
return 0
sum = 0
while i > 0 and j > 0:
sum += i % 10
sum += j % 10
i = i/10
j = j/10
return sum
if __name__ == '__main__':
test = MovingCount()
print(test.movingCount(4, 2, 2))