题解 | #机器人的运动范围#

机器人的运动范围

http://www.nowcoder.com/practice/6e5207314b5241fb83f2329e89fdecc8

public class Solution { int total = 0; public int movingCount(int threshold, int rows, int cols) { boolean[][] map = new boolean[rows][cols]; func(threshold,rows,cols,0,0,map);

	return total;
}

public void func(int threshold, int rows, int cols,int x,int y,boolean[][] map) {
	if(!map[x][y]) {
		if(count(x) + count(y) <= threshold) {
			total++;
			map[x][y] = true;
			if(x > 0) func(threshold,rows,cols,x-1,y,map);
			if(y > 0) func(threshold,rows,cols,x,y-1,map);
			if(x < rows-1) func(threshold,rows,cols,x+1,y,map);
			if(y < cols-1) func(threshold,rows,cols,x,y+1,map);
		}else map[x][y] = true;
	}else return;
	
	return;
}

public int count(int num) {
	int n = 0;
	while(num >= 10) {
		n += num % 10;
		num /= 10;
	}
	n += num;
	
	return n;
}

}

全部评论

相关推荐

牛至超人:哈工大已经很棒了,不需要加括号了,然后咋没有实习经历呢?火速趁寒假整一段实习,导师不让就狠狠肘击
投了多少份简历才上岸
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务