您的位置:首页 > Web前端

机器人的运动范围 剑指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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: