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
| int peak_height(std::vector<std::string>& A) { int n = A.size(), m = A[0].size(); vector<vector<bool>> vis(n,vector<bool>(m)); queue<pair<int,int>> q; int dy[4]{-1,0,1,0}, dx[4]{0,1,0,-1}; for(int i = 0; i < n; i++) { for(int j = 0; j < m; j++) { if(A[i][j] == '^') { bool ok = false; for(int r = 0; r < 4; r++) { int ny = i + dy[r], nx = j + dx[r]; if(ny < 0 or nx < 0 or ny == n or nx == m or A[ny][nx] == ' ') ok = true; } if(ok) { vis[i][j] = true; q.push({i,j}); } } } } if(q.empty()) return 0; int res = 1; while(q.size()) { int sz = q.size(); while(sz--) { auto [y,x] = q.front(); q.pop(); for(int i = 0; i < 4; i++) { int ny = y + dy[i], nx = x + dx[i]; if(0 <= ny and ny < n and 0 <= nx and nx < m and A[ny][nx] == '^') { if(!vis[ny][nx]) { q.push({ny,nx}); vis[ny][nx] = true; } } } } if(q.size()) res += 1; } return res; }
|