Rotating the Box

各一个2d数组, 里面有石头, 空格和障碍, 求右转90度后, 石头落下, 数组的状态.

这题先转90度, 然后从下往上看到空格就往上找石头然后swap, 注意检查边界.

class Solution {
public:
    vector<vector<char>> rotateTheBox(vector<vector<char>>& box) {
        int n = box[0].size();
        int m = box.size();
        vector<vector<char>> res = rotate(box, n, m);
        for(int j = 0; j < m; j++) {
            for(int i = n - 1; i >= 0; i--){
                if(res[i][j] == '.'){// find the space
                    int k = i - 1;
                    while(k >= 0 && res[k][j] == '.') // skip all space above
                        k--;
                    if(k >= 0 && res[k][j] == '#')// find the stone
                        swap(res[k][j], res[i][j]); // swap
                }
            }
        }
        return res;
     }
    vector<vector<char>> rotate(vector<vector<char>>& box, int n, int m){
        vector<vector<char>> res(n, vector<char>(m));
        for(int i = 0; i < n; i++){
            for(int j = 0; j < m; j++){
                res[i][j] = box[m - 1 - j][i];
            }
        }
        return res;
    }
};