• 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;
        }
    };
    
  • 相关阅读:
    一个回车键黑掉一台服务器——使用Docker时不要这么懒啊喂
    docker machine介绍和使用
    Docker Engine和Docker Machine介绍
    Nginx服务器之负载均衡策略
    从linux启动到rootfs的挂载分析
    Docker容器的生命周期管理
    docker的使用
    automake,autoconf使用详解
    go语言中文网中的资源
    Go工具和调试详解
  • 原文地址:https://www.cnblogs.com/pk28/p/9462408.html
Copyright © 2020-2023  润新知