c++ dijkstra + union find solution
\nclass Solution {\npublic:\n    int find(vector<int>& a, int i) {\n        if(a[i] == i) {\n            return i;\n        }\n        return a[i] = find(a, a[i
```\nclass Solution {\npublic:\n    int find(vector<int>& a, int i) {\n        if(a[i] == i) {\n            return i;\n        }\n        return a[i] = find(a, a[i]);\n    }\n    \n    void merge(vector<int>& a, vector<int>& r, int i, int j) {\n        int i_ = find(a,i);\n        int j_ = find(a,j);\n        if(i_ == j_) {\n            return; \n        }\n        if(r[i_] > r[j_]) {\n            a[j_] = i_;\n        } else {\n            a[i_] = j_; \n            if(r[i_] == r[j_]) {\n                r[j_]++;\n            }\n        }\n    }\n    \n    bool same(vector<int>& a, int i, int j) {\n        return find(a,i) == find(a,j);\n    }\n    \n    typedef pair<int,pair<int,int>> pipii;\n    const int INF = INT_MAX / 2; \n    int minPushBox(vector<vector<char>>& grid) {\n        const int m = grid.size();\n        const int n = grid[0].size();\n        vector<vector<int>> dp(m*n,vector<int>(m*n, INF));\n        int p = -1; \n        int b = -1; \n        int target = -1; \n        for(int i = 0;i<m;i++) {\n            for(int j = 0;j<n;j++) {\n                if(grid[i][j] == \'S\') {\n                    p = i * n + j; \n                    grid[i][j] = \'.\';\n                } else if(grid[i][j] == \'B\') {\n                    b = i * n + j;\n                    grid[i][j] = \'.\';\n                } else if(grid[i][j] == \'T\') {\n                    target = i * n + j; \n                    grid[i][j] = \'.\';\n                }\n            }\n        }\n        priority_queue<pipii,vector<pipii>,greater<pipii>> q; \n        q.push({0,{p,b}});\n        while(q.size()) {\n            auto value = q.top();\n            q.pop();\n            int w = value.first; \n            p = value.second.first; \n            b = value.second.second; \n            if(b == target) {\n                return w; \n            }\n            if(dp[b][p] <= w) {\n                continue; \n            }\n            dp[b][p] = w;\n            int x = b/n; \n            int y = b%n; \n            grid[x][y] = \'#\';\n            vector<int> a(m*n);\n            vector<int> rank(m*n);\n            for(int i = 0;i<a.size();i++) {\n                a[i] = i; \n            }\n            for(int i = 0;i<m;i++) {\n                for(int j = 0;j<n;j++) {\n                    if(grid[i][j] != \'.\') {\n                        continue; \n                    } \n                    int t = i * n + j; \n                    if(i+1<m && grid[i+1][j] == \'.\') {\n                        merge(a,rank,t,t+n);\n                    }\n                    if(j+1<n && grid[i][j+1] == \'.\') {\n                        merge(a,rank,t,t+1);\n                    }\n                }\n            }\n            int p_x = p / n; \n            int p_y = p % n; \n            if(x+1<m && x - 1>=0 && same(a,b-n,p) && grid[x+1][y] == \'.\') {\n                q.push({w+1,{b,b+n}});\n            }\n            if(x-1>=0 && x + 1 < m && same(a,p,b+n) && grid[x-1][y] == \'.\') {\n                q.push({w+1,{b,b-n}});\n            }\n            if(y+1<n && y-1>=0 && same(a,p,b-1) && grid[x][y+1] == \'.\') {\n                q.push({w+1,{b,b+1}});\n            }\n            if(y-1>=0 && y + 1 < n && same(a,p,b+1) && grid[x][y-1] == \'.\') {\n                q.push({w+1,{b,b-1}});\n            }\n            grid[x][y] = \'.\';\n        }\n        return -1; \n    }\n};\n```
[]