标签:Count rows offer int 机器人 cols threshold fin 范围
题目:
地上有一个m行和n列的方格。一个机器人从坐标0,0的格子开始移动,每一次只能向左,右,上,下四个方向移动一格,但是不能进入行坐标和列坐标的数位之和大于k的格子。 例如,当k为18时,机器人能够进入方格(35,37),因为3+5+3+7 = 18。但是,它不能进入方格(35,38),因为3+5+3+8 = 19。请问该机器人能够达到多少个格子?
代码:
1 class Solution { 2 public: 3 int movingCount(int threshold, int rows, int cols) 4 { 5 bool* finish = new bool[rows*cols](); 6 return Count(threshold, rows, cols, finish, 0, 0 ); 7 } 8 int Count(int threshold, int rows, int cols, bool* fin, int x, int y ){ 9 if(x < 0 || x >=rows || y < 0 || y >= cols || fin[x*cols+y] ||BitSum(x)+BitSum(y) > threshold ) 10 return false; 11 fin[x*cols+y] = true; 12 return Count(threshold, rows, cols, fin, x - 1, y ) 13 + Count(threshold, rows, cols, fin, x + 1, y ) 14 + Count(threshold, rows, cols, fin, x, y - 1 ) 15 + Count(threshold, rows, cols, fin, x, y + 1 ) 16 + 1; 17 } 18 int BitSum(int t) { 19 int count = 0; 20 while(t){ 21 count += t %10; 22 t /= 10; 23 } 24 return count; 25 } 26 };
我的笔记:
从(0,0)开始走,每成功走一步标记当前位置为true,然后从当前位置往四个方向探索, 返回1 + 4 个方向的探索值之和。 探索时,判断当前节点是否可达的标准为:- 1)当前节点在矩阵内;
- 2)当前节点未被访问过;
- 3)当前节点数值满足限制。
标签:Count,rows,offer,int,机器人,cols,threshold,fin,范围 来源: https://www.cnblogs.com/john1015/p/13140931.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。