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 ------------------------------------------------------------------- ...
随机推荐
- Eclipse 常用快捷键清单
罗列了一些常用的快捷键(显红为很实用的快捷键) Ctrl+Shift+L:快速打开所有快捷键列表 一.文件 F2 :快速打开信息提示.重命名F3 :打开声明(同Ctrl+左鼠)F4 :打开类型层次结构 ...
- VM虚拟机内ubuntu无法连接到网络
VM虚拟机内ubuntu无法连接到网络 解决:编辑网络,将网路都删除掉.又一次加入网络桥接和NAT链接. .又一次连接就可以,查看一下ip地址. 方法2: 虚拟机中新装ubuntu 编辑虚拟网络,先恢 ...
- Android採用async框架实现文件上传
页面效果 须要的权限 <uses-permission android:name="android.permission.INTERNET"/> 网络訪问权限; 布局文 ...
- Shader编程教程
2010-05-13 11:37:14| 分类: DirectX 3D学习|举报|字号 订阅 Shader编程教程1-环境光照 您好,欢迎来到XNA Shader教程1.我的名字叫Petri ...
- eclipse 开发 scala
(环境:jdk1.7,scala插件scala-2.1.1.2-site.zip) 1:下载scala插件 http://download.scala-ide.org/sdk/helium/e38/s ...
- md5加密--32位16进制小写
public class ttgameMd5 { public final static String MD5(String str) { char hexDigits[] = { // 用来将字节转 ...
- kafka eagle 使用教程
下载 地址:http://download.smartloli.org/ github:https://github.com/smartloli/kafka-eagle 环境 Windows: 安装J ...
- Oracle 技术支持之现场优化的思维路径
性能调优是每个DBA职业生涯中都能遇到的任务 大到世界五百强的核心系统,小到乡镇企业的进销存,几乎都会有要调优的时候 面对形形色色的系统,林林总总的需求,调优的手段也是丰富多彩 定位问 ...
- oracle中提高order by的性能
1.如果order by columnA,那么在where查询条件中添加条件columnA=value,则oracle内部会过滤order by排序,直接用索引(可以通过execution plan查 ...
- leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...