leetcode 889. Spiral Matrix III
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的更多相关文章
- [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 ...
- LeetCode 885. Spiral Matrix III
原题链接在这里:https://leetcode.com/problems/spiral-matrix-iii/ 题目: On a 2 dimensional grid with R rows and ...
- 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 ...
- [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 ...
- 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 ...
- [LeetCode 题解] Spiral Matrix
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 题目链接 54. Spiral Matrix ...
- LeetCode: 59. Spiral Matrix II(Medium)
1. 原题链接 https://leetcode.com/problems/spiral-matrix-ii/description/ 2. 题目要求 给定一个正整数n,求出从1到n平方的螺旋矩阵.例 ...
- 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 ...
- LeetCode - 54. Spiral Matrix
54. Spiral Matrix Problem's Link ------------------------------------------------------------------- ...
随机推荐
- 安装Glass Box代理程序
安装玻璃盒代理程序 目前版本的玻璃代理程序主要支持主流 Java EE 应用程序服务器(如 JBoss,Tomcat,WebLogic 和 WebSphere).玻璃盒代理程序可以自动化安装,但考虑到 ...
- ganlia安装配置文档
gangliaz在ubuntu中安装和配置很简单 1. 服务器端安装 sudo apt-get install ganglia-monitor ganglia-webfrontend rrdtool ...
- java锁之wait,notify(wait会释放锁,notify仅仅只是通知,不释放锁)
wait是指在一个已经进入了同步锁的线程内,让自己暂时让出同步锁,以便其他正在等待此锁的线程可以得到同步锁并运行,只有其他线程调用了notify方法(notify并不释放锁,只是告诉调用过wait方法 ...
- mapreduce中reduce中的迭代器只能调用一次。其实迭代器就只能调用一次
亲测,只能调用一次,如果想想在一次reduce重复使用迭代器中的数据,得先取出来放在list中然后在从list中取出来!!多次读取reduce函数中迭代器的数据 public static void ...
- iOS左滑手势失效
iOS7之后,苹果优化了一个小功能,就是对于UINavagationController堆栈里的UIViewController,只要轻轻在视图控制器的左边缘右滑一下,该视图控制器就会pop出栈(前提 ...
- 2017湘潭赛 A题 Determinant (高斯消元取模)
链接 http://202.197.224.59/OnlineJudge2/index.php/Problem/read/id/1260 今年湘潭的A题 题意不难 大意是把n*(n+1)矩阵去掉某一列 ...
- 线程池 Future 带返回结果
package com.aibi.cmdc.bigscreen.action; import java.util.ArrayList; import java.util.HashMap; import ...
- 【问题】:spring cloud sleuth日志组件冲突问题
在使用spring cloud sleuth的时候,启动工程报错如下: 根据错误信息明显就是jar包冲突,spring boot默认用的是logback,所以移除其中一个依赖就可以了,修改pom依赖为 ...
- jquery实现全选、全消、反选功能
HTML代码: <input type="checkbox" name="checkbox" class="A" /> 使用按钮 ...
- Laravel开发:Laravel核心——Ioc服务容器源码解析(服务器绑定)
服务容器的绑定 bind 绑定 bind 绑定是服务容器最常用的绑定方式,在 上一篇文章中我们讨论过,bind 的绑定有三种: 绑定自身 绑定闭包 绑定接口 今天,我们这篇文章主要从源码上讲解 Ioc ...