0062.cpp (650B)
1 class Solution { 2 public: 3 int uniquePaths(int n, int m) { 4 vector<vector<int>> mat(n, vector<int>(m, 0)); 5 queue<pair<int, int>> q; 6 q.push({n - 1, m - 1}), mat[n - 1][m - 1] = 1; 7 while (!q.empty()) { 8 auto [x, y] = q.front(); 9 q.pop(); 10 if (x - 1 >= 0) { 11 mat[x - 1][y] += mat[x][y]; 12 if (mat[x - 1][y] == mat[x][y]) q.push({x - 1, y}); 13 } 14 if (y - 1 >= 0) { 15 mat[x][y - 1] += mat[x][y]; 16 if (mat[x][y - 1] == mat[x][y]) q.push({x, y - 1}); 17 } 18 } 19 return mat[0][0]; 20 } 21 };