机器人的运动范围 剑指offer66题
2017-05-15 20:43
337 查看
include "stdafx.h"
#include<vector> #include<algorithm> #include<string> #include<iostream> #include<stack> using namespace std; class Solution { public: int getSum(int rows, int cols) { int sum = 0; while (rows!=0) { sum += rows % 10; rows = rows / 10; } while (cols != 0) { sum += cols % 10; cols = cols / 10; } return sum; } int count = 0; int movingCount(int threshold, int rows, int cols) { vector<vector<bool>> visited(rows,vector<bool>(cols,false)); getCount(threshold, 0, 0, visited, rows, cols); return count; } void getCount(int threshold, int rows, int cols, vector<vector<bool>> &visited,int m,int n) { if (getSum(rows, cols) <= threshold) { count++; // cout << count << endl; visited[rows][cols] = true; if (cols - 1 >= 0&&visited[rows][cols-1]==false) { getCount(threshold, rows, cols - 1, visited,m,n); } if (cols + 1 < n && visited[rows][cols + 1]==false) { getCount(threshold, rows, cols + 1, visited,m,n); } if (rows - 1 >= 0 && visited[rows-1][cols]==false) { getCount(threshold, rows-1, cols , visited,m,n); } if (rows + 1 < m && visited[rows + 1][cols]==false) { getCount(threshold, rows + 1, cols, visited,m,n); } // visited[rows][cols] = false; //count--; } } }; int main() { Solution s; cout << s.movingCount(5, 10, 10) << endl; return 0; }
相关文章推荐
- [剑指offer]机器人的运动范围
- 61.机器人的运动范围
- 机器人的运动范围
- 机器人的运动范围
- java-机器人的运动范围
- 剑指offer:机器人的运动范围
- 《剑指offer》13.机器人的运动范围
- 机器人的运动范围:回溯法-->能用递归就不用栈。
- 【day-9】剑指Offer-机器人的运动范围
- 面试题67. 机器人的运动范围
- 机器人的运动范围
- 机器人的运动范围
- 算法系列——机器人的运动范围
- 剑指offer面试题67:机器人的运动范围
- 剑指offer-----机器人的运动范围 java
- 机器人的运动范围
- 机器人的运动范围
- 剑指Offer-13:机器人的运动范围
- 66机器人的运动范围
- 面试题67 机器人的运动范围