• 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;
        }
    };
    
  • 相关阅读:
    ACM HDU 3910 Liang Guo Sha(数学题,读懂题目)
    防止 7Zip 生成的 ZIP 文件在 Mac OS X 下出现乱码
    NYOJ 506
    Scanner
    String 与StringBuilder
    基于JAVA的聊天室开发
    PS加粗字体
    MySQL相关命令
    Matlab中数据处理和多项式插值与曲线拟合
    dos下进入某一文件
  • 原文地址:https://www.cnblogs.com/pk28/p/9462408.html
Copyright © 2020-2023  润新知