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

机器人的运动范围

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

}

全部评论

相关推荐

双尔:反手回一个很抱歉,经过慎重考虑,您与我的预期暂不匹配,感谢您的投递
点赞 评论 收藏
分享
11-04 19:05
已编辑
东莞城市学院 单片机
不知道怎么取名字_:你这个要实习两年?哪有这么久的,感觉就是即使你毕业了,但还按实习的话,是不是不用给你缴社保公积金啥的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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