-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1706-where-will-the-ball-fall.cpp
More file actions
21 lines (21 loc) · 982 Bytes
/
Copy path1706-where-will-the-ball-fall.cpp
File metadata and controls
21 lines (21 loc) · 982 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
vector<int> findBall(vector<vector<int>>& grid) {
vector<int> res;
for(int i=0; i<grid[0].size(); i++){
pair<int, int> curr = {0, i};
bool stuck = false;
while(!stuck && curr.first<grid.size()-1){
int currVal = grid[curr.first][curr.second];
if(curr.second+currVal<grid[0].size() && curr.second+currVal>=0 && currVal*grid[curr.first][curr.second+currVal]==1)
curr.first++, curr.second+=currVal;
else stuck = true;
// cout << "(" << curr.first << "," << curr.second << ")\n";
}
// cout << "---" << endl;
int currVal = grid[curr.first][curr.second];
res.push_back(stuck || curr.second+currVal>=grid[0].size() || curr.second+currVal<0 || currVal*grid[curr.first][curr.second+currVal]!=1?-1:curr.second+grid[curr.first][curr.second]);
}
return res;
}
};