zoukankan      html  css  js  c++  java
  • 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]]
    

    1

    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]]
    
    

    2

    思路: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;
        }
    };
    
  • 相关阅读:
    Django简介和安装
    CVE-2011-0104:Microsoft Office Excel 中的栈溢出漏洞调试分析
    Struts2漏洞
    JSONP跨域资源共享的安全问题
    如何以最简单的方式安装 KALI 渗透测试框架系统
    CVE-2010-3333:Microsoft RTF 栈溢出漏洞调试分析
    CVE-2010-2883:基于样本分析 PDF SING表字符溢出漏洞
    针对缓冲区保护技术(ASLR)的一次初探
    利用 ROP 技术绕过 DEP 保护的一次简单尝试
    缓冲区溢出之栈溢出利用(手动编写无 payload 的 Exploit)
  • 原文地址:https://www.cnblogs.com/pk28/p/9462408.html
Copyright © 2011-2022 走看看