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0,0,0,0,0,0,0,0,0,0,0,0,1,0\n1,0,0,0,0,0,1,0,0,0,1,0,0,0,1\n0,0,1,0,1,0,0,0,0,0,0,0,0,0,0\nmy code is as follow:\nclass Solution {\npublic:\n    int minimumMoves(std::vector<std::vector<int>> const & grid) {\n        int rowSize = grid.size();\n        if (0 == rowSize) {\n            return 0;\n        }\n\n        int colSize = grid[0].size();\n        if (0 == colSize) {\n            return 0;\n        }\n\n        std::unordered_map<int, int> visited;\n        std::queue<int> todo;\n        int code = encode(0, 1, 0, 0);\n        todo.emplace(code);\n        visited[code] = 0;\n        int res = INT_MAX;\n        while (todo.size()) {\n            int cur = todo.front(); todo.pop();\n            if (visited[cur] > res) {\n                continue;\n            }\n            int headRow = 0, headCol = 0, endRow = 0, endCol = 0;\n            decode(cur, headRow, headCol, endRow, endCol);\n            if (headRow == rowSize - 1 && headCol == colSize - 1 &&\n                    endRow == headRow && endCol == headCol - 1) {\n                res = std::min(res, visited[cur]);\n            }\n            if (canRight(grid, headRow, headCol, endRow, endCol)) {\n                int newCode = encode(headRow, headCol, endRow, endCol);\n                if (visited.find(newCode) == visited.end() || visited[newCode] > visited[cur] + 1) {\n                    todo.emplace(newCode);\n                    visited[newCode] = visited[cur] + 1;\n                }\n            }\n\n            decode(cur, headRow, headCol, endRow, endCol);\n            if (canDown(grid, headRow, headCol, endRow, endCol)) {\n                int newCode = encode(headRow, headCol, endRow, endCol);\n                if (visited.find(newCode) == visited.end() || visited[newCode] > visited[cur] + 1) {\n                    todo.emplace(newCode);\n                    visited[newCode] = visited[cur] + 1;\n                }\n            }\n\n            decode(cur, headRow, headCol, endRow, endCol);\n            if (canClockWise(grid, headRow, headCol, endRow, endCol)) {\n                int newCode = encode(headRow, headCol, endRow, endCol);\n                if (visited.find(newCode) == visited.end() || visited[newCode] > visited[cur] + 1) {\n                    todo.emplace(newCode);\n                    visited[newCode] = visited[cur] + 1;\n                }\n            }\n\n            decode(cur, headRow, headCol, endRow, endCol);\n            if (canCounterClockWise(grid, headRow, headCol, endRow, endCol)) {\n                int newCode = encode(headRow, headCol, endRow, endCol);\n                if (visited.find(newCode) == visited.end() || visited[newCode] > visited[cur] + 1) {\n                    todo.emplace(newCode);\n                    visited[newCode] = visited[cur] + 1;\n                }\n            }\n        }\n        return INT_MAX == res ? -1 : res;\n    }\nprivate:\n    int encode(int headRow, int headCol, int endRow, int endCol) {\n        return (headRow << 24) | (headCol << 16) | (endRow << 8) | (endCol);\n    }\n\n    void decode(int res, int & headRow, int & headCol, int & endRow, int & endCol) {\n        headRow = (res >> 24) & 0xFF;\n        headCol = (res >> 16) & 0xFF;\n        endRow = (res >> 8) & 0xFF;\n        endCol = res & 0xFF;\n    }\n\n    bool canRight(std::vector<std::vector<int>> const & grid,\n            int & headRow, int & headCol, int & endRow, int & endCol) {\n        int newHeadCol = headCol + 1;\n        if (newHeadCol >= grid[headRow].size() || 1 == grid[headRow][newHeadCol]) {\n            return false;\n        }\n        int newEndCol = newHeadCol;\n        if (headCol == endCol) {\n            if (1 == grid[endRow][newEndCol]) {\n                return false;\n            }\n        } else {\n            newEndCol = headCol;\n        }\n        headCol = newHeadCol;\n        endCol = newEndCol;\n        return true;\n    }\n\n    bool canDown(std::vector<std::vector<int>> const & grid,\n            int & headRow, int & headCol, int & endRow, int & endCol) {\n        int newHeadRow = headRow + 1;\n        if (newHeadRow >= grid.size() || 1 == grid[newHeadRow][headCol]) {\n            return false;\n        }\n        int newEndRow = newHeadRow;\n        if (headRow == endRow) {\n            if (1 == grid[newEndRow][endCol]) {\n                return false;\n            }\n        } else {\n            newEndRow = headRow;\n        }\n        headRow = newHeadRow;\n        endRow = newEndRow;\n        return true;\n    }\n\n    bool canClockWise(std::vector<std::vector<int>> const & grid,\n            int & headRow, int & headCol, int & endRow, int & endCol) {\n        if (headRow != endRow) {\n            return false;\n        }\n        int newHeadRow = endRow + 1;\n        if (newHeadRow >= grid.size() || 1 == grid[newHeadRow][endCol]) {\n            return false;\n        }\n        headRow = newHeadRow;\n        headCol = endCol;\n        return true;\n    }\n\n    bool canCounterClockWise(std::vector<std::vector<int>> const & grid,\n            int & headRow, int & headCol, int & endRow, int & endCol) {\n        if (headCol != endCol) {\n            return false;\n        }\n        int newHeadCol = endCol + 1;\n        if (newHeadCol >= grid[endRow].size() || 1 == grid[endRow][newHeadCol]) {\n            return false;\n        }\n        headRow = endRow;\n        headCol = newHeadCol;\n        return true;\n    }\n};
[]