aboutsummaryrefslogtreecommitdiff
path: root/where_will_ball_fall.cpp
blob: 41957783b4719e874bc0e8c3490afb1b7f7ec979 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include "leetcode.h"

class Solution {
public:
  vector<int> findBall(vvd & grid) {
    int m = grid.size(), n = grid[0].size();
    vvd ugrid(m * 3, vector<int>(n * 3));
    for (auto i = 0; i < m; i++)
      for (auto j = 0; j < n; j++) {
        ugrid[i * 3 + 1][j * 3 + 1] = 1;
        ugrid[i * 3][j * 3] = ugrid[i * 3 + 2][j * 3 + 2] = grid[i][j] == 1;
        ugrid[i * 3][j * 3 + 2] = ugrid[i * 3 + 2][j * 3] = grid[i][j] != 1;
      }
    vector<int> ans;
    for (auto i = 0; i < n; ++i)
      ans.push_back(drop(i * 3 + 1, ugrid, m * 3, n * 3));
    return ans;
  }

private:
  vector<pair<int, int>> dir{{1, 0}, {0, -1}, {0, 1}};
  int drop(int pos, vvd & ugrid, int m, int n) {
    queue<pair<int, int>> qp;
    qp.push({0, pos});
    while (!qp.empty()) {
      auto [i, j] = qp.front();
      qp.pop();
      if (i == m - 1)
        return j / 3;
      ugrid[i][j] = 1;
      for (auto [di, dj] : dir)
        if (j + dj >= 0 && j + dj < n && !ugrid[i + di][j + dj]) {
          qp.push({i + di, j + dj});
          break;
        }
    }
    return -1;
  }
};

int main() {
  Solution obj;
  vvd grid = {{1, 1, 1, -1, -1},
                   {1, 1, 1, -1, -1},
                   {-1, -1, -1, 1, 1},
                   {1, 1, 1, 1, -1},
                   {-1, -1, -1, -1, -1}};
  for (auto i : obj.findBall(grid))
    cout << i << ' ';
}