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. git个人使用总结(命令版)

    一.基础命令 快照类操作:add.status.diff.commit.reset.rm.mv 分支类基本操作:branch.checkout.log.stash 分享及更新项目基本操作:pull.p ...

  2. html禁止图片拖拽移动在新窗口打开

    一直觉得直接从网站的表格上复制数据挺方便的, 今天,领导突然说网站上的图片可以被别人拖走了,必须禁止,哎,果然只有领导才考虑得到这种事情啊 so, 将ondragstart="return ...

  3. vivado2013.4和modelsim联合仿真

    vivado2013.4和modelsim联合仿真                           Hello,Panda        最近在做Zynq的项目,曾经尝试使用ISE+PlanAhe ...

  4. poj 2553 The Bottom of a Graph(强连通、缩点、出入度)

    题意:给出一个有向图G,寻找所有的sink点.“sink”的定义为:{v∈V|∀w∈V:(v→w)⇒(w→v)},对于一个点v,所有能到达的所有节点w,都能够回到v,这样的点v称为sink. 分析:由 ...

  5. 我的_vimrc文件

    """"""""""""""""&quo ...

  6. iOS swift NSClassFromString将字符串转换成类名

    在oc中将字符串转换成类名直接调用NSClassFromString("classname")即可,但是到了swift中变的麻烦多了 swift中如果要将字符串转换为类型需要以下几 ...

  7. shell常用操作积累

    1. 拼接字符串* #!/bin/sh write_log(){ local up_name=$ local num=${#string} ]; do up_name="$up_name*& ...

  8. Photoshop中磁力套索的一种简陋实现(基于Python)

    经常用Photoshop的人应该熟悉磁力套索(Magnetic Lasso)这个功能,就是人为引导下的抠图辅助工具.在研发领域一般不这么叫,通常管这种边缘提取的办法叫Intelligent Sciss ...

  9. 【Mac系统】之fiddler下载和安装

    使用教程参考:http://www.cnblogs.com/TankXiao/archive/2012/02/06/2337728.html#request 一.首先,在Mac下安装fiddler时, ...

  10. Python 内建的filter()函数用于过滤序列。

    例如,在一个list中,删掉偶数,只保留奇数,可以这么写: def is_odd(n): return n % 2 == 1 list(filter(is_odd, [1, 2, 4, 5, 6, 9 ...