机器人的运动范围 剑指offer66题

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;
}
posted @ 2017-05-15 20:43  wdan2016  阅读(149)  评论(0编辑  收藏  举报