On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east.

Here, the north-west corner of the grid is at the first row and column, and the south-east corner of the grid is at the last row and column.

Now, we walk in a clockwise spiral shape to visit every position in this grid.

Whenever we would move outside the boundary of the grid, we continue our walk outside the grid (but may return to the grid boundary later.)

Eventually, we reach all R * C spaces of the grid.

Return a list of coordinates representing the positions of the grid in the order they were visited.

Example 1:

Input: R = 1, C = 4, r0 = 0, c0 = 0
Output: [[0,0],[0,1],[0,2],[0,3]]

Example 2:

Input: R = 5, C = 6, r0 = 1, c0 = 4
Output: [[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]]

思路:8个方向去找,比如第一个位置1,找他的8个方向,第二个位置2,也去找他的8个方向,但是8个方向的查找次序要从(1->2)这个方向开始,这样能保证顺时针的顺序。

class Solution {
public:
int dir[16][2] = {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
//int dir2[8][2] = {0,-1,-1,-1,-1,0,-1,1,0,1,1,1,1,0,1,-1};
vector<vector<int> > spiralMatrixIII(int R, int C, int r0, int c0) {
vector<vector<int> > ans;
queue<pair<int,int> >q;
map<pair<int,int>, int> mp;
ans.push_back({r0, c0});
mp[{r0, c0}] = 1;
map<pair<int,int>, pair<int,int> > mp2;
for (int i = 0; i < 8; ++i) {
int x = r0 + dir[i][0];
int y = c0 + dir[i][1];
if (!mp[{x,y}] && x < R && y < C && x >= 0 && y >= 0) {
mp2[{x,y}] = {dir[i][0],dir[i][1]};
q.push({x,y});
mp[{x,y}] = 1;
}
}
while (!q.empty()) {
pair<int,int> u = q.front(); q.pop();
ans.push_back({u.first, u.second});
int i = 0;
pair<int,int> p = mp2[{u.first,u.second}];
int ox = -p.first;
int oy = -p.second;
//cout << "NO";
//cout << ox << " " << oy << endl;
int mark = 0;
while (i < 16) {
if (dir[i][0] == ox && dir[i][1] == oy) {
mark = i;
break;
}
++i;
}
for (; i < mark+8; ++i) {
//cout << i << endl;
int x = u.first + dir[i][0];
int y = u.second + dir[i][1];
if (!mp[{x,y}] && x < R && y < C && x >= 0 && y >= 0) {
q.push({x,y});
mp2[{x,y}] = {dir[i][0],dir[i][1]};
mp[{x,y}] = 1;
}
}
}
return ans;
}
};

leetcode 889. Spiral Matrix III的更多相关文章

  1. [LeetCode] 885. Spiral Matrix III 螺旋矩阵之三

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  2. LeetCode 885. Spiral Matrix III

    原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...

  3. Java for LeetCode 059 Spiral Matrix II

    Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order. For ...

  4. [LeetCode] 59. Spiral Matrix II 螺旋矩阵 II

    Given an integer n, generate a square matrix filled with elements from 1 to n^2 in spiral order. For ...

  5. 885. Spiral Matrix III

    On a 2 dimensional grid with R rows and C columns, we start at (r0, c0) facing east. Here, the north ...

  6. [LeetCode 题解] Spiral Matrix

    前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...

  7. LeetCode: 59. Spiral Matrix II(Medium)

    1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...

  8. Leetcode 54. Spiral Matrix & 59. Spiral Matrix II

    54. Spiral Matrix [Medium] Description Given a matrix of m x n elements (m rows, n columns), return ...

  9. LeetCode - 54. Spiral Matrix

    54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...

随机推荐

  1. linux路由服务

    本文介绍怎样使用linux创建一台简单的路由server. 主要包含几个參数的设置:ip_forward和rp_filter. 1.开启IP forwarding # 重新启动后失效 $ echo & ...

  2. linux程序设计——取消一个线程(第十二章)

    12.7    取消一个线程 有时,想让一个线程能够要求还有一个线程终止,就像给它发送一个信号一样. 线程有方法能够做到这一点,与与信号处理一样.线程能够被要求终止时改变其行为. pthread_ca ...

  3. cdn日志统一下载程序

    最近实现了网宿cdn,阿里云cdn,腾讯cdn的日志统一格式下载程序,使用简单方便,有需要详见代码: https://github.com/mikeluwen/CdnLogDownload

  4. vbox克隆虚拟机,网卡启动报错“Device eth0 does not seem to be present”

    vbox克隆虚拟机,网卡启动报错"Device eth0 does not seem to be present". 须要看以下三个地方:确保文件名称,设备名.mac地址都一致. ...

  5. JQuery小结(转)

    一.页面加载 JQ的页面加载比JS要快,当整个dom树结构生成完毕后就会加载 JQ页面加载不存在覆盖问题,加载的时候是顺序执行 JQ的页面加载最简写的方式为: $(function(){ alert( ...

  6. lua math 库

    lua math库 (2012-05-18 17:26:28) 转载▼ 标签: 游戏 分类: Lua atan2.sinh.cosh.tanh这4个应该用不到. 函数名 描述 示例 结果 pi 圆周率 ...

  7. swift - 实现类似今日头条顶部标签和底部内容的动态解决方案

    TYPageView TYPageView 类似今日头条 的标签导航解决方案,支持多种样式选择,基于swift3.0,支持文字颜色动态变化,底部选中线的动态变化 配图: 使用方法: let title ...

  8. 今天学习Ibatis,花了我一个下午的时间,程序猿呀,你上点心吧

    今天花了半天的时间完成了一个小小小的项目 烦了两个错误:第一个没有对Dao层进行实例化, 第二个错误是: 给数据表其错了名字,现在很混乱呀 不能其Content相似的名字呀! 还是等心情平复了再写日记 ...

  9. Hadoop2.6.0版本MapReudce示例之WordCount(一)

    一.准备测试数据 1.在本地Linux系统/var/lib/Hadoop-hdfs/file/路径下准备两个文件file1.txt和file2.txt,文件列表及各自内容如下图所示: 2.在hdfs中 ...

  10. Android Studio SDK Manager 解决无法更新问题

    一.首先要保证你可以FQ上google等网站. 这个..如何越过GFW就要靠自己了..网上也有很多教程.. 二.更改android sdk manager的option设置 选择Tools→Opini ...