885. 螺旋矩阵 III

在 R 行 C 列的矩阵上,我们从 (r0, c0) 面朝东面开始

这里,网格的西北角位于第一行第一列,网格的东南角位于最后一行最后一列。

现在,我们以顺时针按螺旋状行走,访问此网格中的每个位置。

每当我们移动到网格的边界之外时,我们会继续在网格之外行走(但稍后可能会返回到网格边界)。

最终,我们到过网格的所有 R * C 个空间。

按照访问顺序返回表示网格位置的坐标列表。

*************************************************************************************************************************************************

用程序求阿基米德线的一部分

暴力解法参考:

螺旋矩阵 III(failed)

本方法:

如果矩阵坐标按照规则增加越界,自动寻找下一个矩阵坐标范围内的起始点

示例 1:

输入:R = 1, C = 4, r0 = 0, c0 = 0
输出:[[0,0],[0,1],[0,2],[0,3]]

示例 2:

输入:R = 5, C = 6, r0 = 1, c0 = 4
输出:[[1,4],[1,5],[2,5],[2,4],[2,3],[1,3],[0,3],[0,4],[0,5],[3,5],[3,4],[3,3],[3,2],[2,2],[1,2],[0,2],[4,5],[4,4],[4,3],[4,2],[4,1],[3,1],[2,1],[1,1],[0,1],[4,0],[3,0],[2,0],[1,0],[0,0]]

提示:

  1. 1 <= R <= 100
  2. 1 <= C <= 100
  3. 0 <= r0 < R
  4. 0 <= c0 < C
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <deque>
#include <map>
using namespace std;


class Solution 
{
private:
	int limit_up, limit_down, limit_left, limit_right;
	int rmax, cmax;
	bool confine = false;
	int count = 1;
public:
	vector<vector<int>> spiralMatrixIII(int R, int C, int r0, int c0)
	{
		rmax = R - 1;
		cmax = C - 1;
		limit_up = r0, limit_down = r0, limit_left = c0, limit_right = c0;
		vector<pair<int, int>>temp;
		vector<vector<int>> ans;
		temp.push_back({ r0,c0 });
		cout << count << ": " << r0 << " " << c0 << endl;
		int s;
		for (int i = 1; i < 5; i++)
		{
			if (temp.size() == R * C)
				break;
			s = i;
			switch (s)
			{
			case 1:
				if (confine)
				{
					if (limit_right != cmax)
					{
						c0 = limit_right + 1;
						limit_right++;
						confine = 0;
						count++, cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					}
					else
					{
						c0 = limit_right;
					}
					break;
				}
				else
					while (c0 < C )
				{

					c0++;
					//step1:
					
					if (c0 < C)
						count++, cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					if (c0 - limit_right == 1 && limit_right < C-1)
					{
						limit_right = c0;
						break;
					}
					else
					{
						trans(r0, c0, i);
						if (i != s)
							break;
					}
				}
				break;

			case 2:
				if (confine)
				{
					if (limit_down!=rmax)
					{
						r0 = limit_down + 1;
						limit_down++;
						confine = 0;
						count++,cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					}
					else
					{
						r0 = limit_down;
					}
					break;
				}
				else
					while (r0 < R )
				{
					r0++;
					//step2:
					if (r0 < R)
						count++,cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					if (r0 - limit_down == 1 && limit_down < R-1)
					{
						limit_down = r0;
						break;
					}
					else
					{
						trans(r0, c0, i);
						if (i != s)
							break;
					}
				}
				break;

			case 3:
				if (confine)
				{
					if (limit_left!=0)
					{
						c0 = limit_left - 1;
						limit_left--;
						confine = 0;
						count++, cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					}
					else
					{
						c0 = 0;
					}
					break;
				}
				else
					while (c0 > -1)
				{
					c0--;
					//step3:
					if (c0 > -1)
						count++,cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });					
					if (limit_left - c0 == 1 && limit_left > 0)
					{
						limit_left = c0;
						break;
					}
					else
					{
						trans(r0, c0, i);
						if (i != s)
							break;
					}
				}
				break;
			case 4:
				if (confine)
				{
					if (limit_up!=0)
					{
						r0 = limit_up - 1;
						limit_up--;
						confine = 0;
						count++, cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					}
					else
					{
						r0 = 0;
					}
					break;
				}
				else
					while (r0 > -1)
				{
					r0--;
					if (r0 > -1)
						count++,cout << count << ": " << r0 << " " << c0 << endl, temp.push_back({ r0,c0 });
					if (limit_up - r0 == 1 && limit_up > 0)
					{
						limit_up = r0;
						break;
					}
					else
					{
						trans(r0, c0, i);
						if (i != s)
							break;
					}
				}
				break;

			}
			if (i != s)
				i--;
			if (i == 4)
				i = 0;
		}
		for (auto i = temp.begin(); i !=temp.end(); i++)
		{
			pair<int, int>te = *i;
			ans.push_back({ te.first,te.second });
		}
		return ans;
	}
	void trans( int &r0,  int &c0,int &i)
	{
		if (c0>cmax && i == 1)
		{
			c0 = cmax;
			confine = true;
			i++;
		}
		else if (r0>rmax && i == 2)
		{
			r0 = rmax;
			confine = true;
			i++;
		}
		else if (c0<0 && i == 3)
		{
			c0 = 0;
			confine = true;
			i++;
		}
		else if (r0<0 && i == 4)
		{
			r0 = 0;
			confine = true;
			i++;
		}
	}
};


int main()
{
	int R = 1, C = 4, r0 = 0, c0 = 0;

	Solution s;
	s.spiralMatrixIII(R, C, r0, c0);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/kongqingxin12/article/details/83065761