zoukankan      html  css  js  c++  java
  • 机器人的运动范围 剑指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;
    }
  • 相关阅读:
    C# richTextBox封装的一个打印的类
    RichtextBox打印
    RichTextBox选中文本时往自己的其他的位置实现拖拽
    C# 保存和读取TreeView展开的状态
    RichtextBox去除闪烁光标
    自己重启自己
    记录一次shell里局部变量的问题
    Redis配置总结
    Nginx原理和配置总结
    CentOS7+Nginx+多个Tomcat配置
  • 原文地址:https://www.cnblogs.com/wdan2016/p/6858301.html
Copyright © 2011-2022 走看看