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 ------------------------------------------------------------------- ...
随机推荐
- checked exception和unchecked exception区别
http://blog.csdn.net/yuefengyuan/article/details/6204317 一. Java 中定义了两类异常: 1) Checked exception: 这类异 ...
- JDBC技术总结(三)
1. 数据库连接池 JDBC部分的前两个总结主要总结了一下JDBC的基本操作,而且有个共同点,就是应用程序都是直接获取数据库连接的.这会有个弊端:用户每次请求都需要向数据库获得连接,而数据库创建连接通 ...
- Python修改文件权限
os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...
- Android+git+hudson+gradle持续集成
linux 主机 android sdk安装忽略 jdk安装忽略 hudson安装忽略 gradle安装 1:下载相应的gradle(这里是gradle-2.10-all.zip) 2 : 解 ...
- Java利用Axis远程调用WebService接口
准备工作: 主要依赖的包: 1.axis.jar 官网:http://axis.apache.org/axis/ 2.jaxrpc.jar 下载地址:http://www.java2s.com/Cod ...
- 篇章一:[AngularJS] 使用AngularAMD動態載入Controller
前言 使用AngularJS來開發Single Page Application(SPA)的時候,可以選用AngularUI Router來提供頁面內容切換的功能.但是在UI Router的使用情景裡 ...
- Sqlserver建立Oracle的鏈接服務器
--建立数据库链接服务器 EXEC sp_addlinkedserver @server =N'TestOracle', --要创建的链接服务器别名 @srvproduct=N'Oracle', -- ...
- listView的异步加载数据
1 public class MainActivity extends Activity { 2 3 private ListView listView; 4 private ArrayList< ...
- 为div添加滚动效果:
为div添加滚动效果: .xxxx{ width: 100%; height: 100%; overflow: hidden; overflow-y: auto;} 代码片段 <div clas ...
- 【Mac系统】之Mysql数据库遇到修改数字密码的问题(SQL语法错误:ERROR 1064 (42000),密码策略等问题:ERROR 1819 (HY000))
安装完Mysql也进行了第一次初始化密码以及修改密码规则(请参考文章),但是我想后续再改密码,出现了下面几个问题: #SQL语句错误问题 ERROR 1064 (42000): You have an ...