Ultraviolet A 12130-Summits (BFS + greedy)
Ultraviolet A 12130-Summits
Question Link
Question: given an h * w graph, each position has a value. Now we need to determine the number of peaks and peaks on this graph. The peak is defined as this. There is a d value. If a position is the top of the peak, it cannot go to a position not greater than the height of the top-d. If this condition is met, and cannot reach a higher mountain.
Idea: Use the greedy strategy to drop all vertices to the priority queue. Each time the highest peak value is retrieved, start searching and perform extensive searches. the maximum number of vertices is recorded in the search process, if this is the peak, add this number. The method for determining whether it is the peak is: if the maximum peak value of a point cannot be found during the wide search process, it is the peak, you can record the maximum height that each vertex can reach during the wide search process. Then, in fact, each vertex can only be searched once, complexity: O (hw log (h * w ))
Code:
#include
#include
#include
#include
using namespace std;const int N = 505;const int D[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};int t, h, w, d, g[N][N], vis[N][N];struct Point { int x, y, val; Point(int x, int y, int val = 0) {this->x = x;this->y = y;this->val = val; } bool operator < (const Point& a) const {return val < a.val; }};priority_queue
Q;int bfs(int x, int y, int H) { queue
Q; Q.push(Point(x, y)); vis[x][y] = H; int ans = 1; int flag = 1; while (!Q.empty()) {Point u = Q.front();Q.pop();for (int i = 0; i < 4; i++) { int xx = u.x + D[i][0]; int yy = u.y + D[i][1]; if (xx < 0 || xx >= h || yy < 0 || yy >= w) continue; if (H - g[xx][yy] >= d) continue; if (vis[xx][yy] > H) {flag = 0;continue; } if (vis[xx][yy] == H) continue; if (g[xx][yy] == H) ans++; vis[xx][yy] = H; Q.push(Point(xx, yy));} } if (flag) return ans; return 0;}int solve() { memset(vis, -1, sizeof(vis)); int ans = 0; while (!Q.empty()) {Point u = Q.top();Q.pop();if (vis[u.x][u.y] != -1) continue;ans += bfs(u.x, u.y, g[u.x][u.y]); } return ans;}int main() { scanf("%d", &t); while (t--) {scanf("%d%d%d", &h, &w, &d);for (int i = 0; i < h; i++) { for (int j = 0; j < w; j++) {scanf("%d", &g[i][j]);Q.push(Point(i, j, g[i][j])); }}printf("%d\n", solve()); } return 0;}