uva 12130 - Summits(BFS)
来源:互联网 发布:ubuntu安装tar.gz 编辑:程序博客网 时间:2024/05/29 12:46
题目链接:uva 12130 - Summits
题目大意:给定一个N∗M的图,每个位置有一个值。给定D,表示有节点值为G的位置作为起点的话,将不能移动到值小于等于G−D的节点。现在要求找到整个图中所有的峰值点,峰值点的定义是不能移动到比自己节点值大的位置。
解题思路:将每个位置按照权值排序,逐个作为起点进行移动,v[x][y]数组值可以到达x,y的节点的值的最大值,如果起始点可以移动到v[x][y]大于本身的点,则说明该起点不是峰值点。
#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int maxn = 505;const int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};struct point { int x, y, val; point (int x = 0, int y = 0, int val = 0) { this->x = x; this->y = y; this->val = val; } bool operator < (const point& u) const { return val > u.val; }};int N, M, D, g[maxn][maxn], v[maxn][maxn];vector<point> vec;void init () { vec.clear(); memset(v, 0, sizeof(v)); scanf("%d%d%d", &N, &M, &D); for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { scanf("%d", &g[i][j]); vec.push_back(point(i, j, g[i][j])); } } sort(vec.begin(), vec.end());}int bfs (int x, int y, int h) { int ret = 1; bool flag = true; queue<point> que; que.push(point(x, y)); while (!que.empty()) { point u = que.front(); que.pop(); for (int i = 0; i < 4; i++) { int p = u.x + dir[i][0]; int q = u.y + dir[i][1]; if (p <= 0 || p > N || q <= 0 || q > M) continue; if (h - g[p][q] >= D) continue; if (v[p][q] > h) { flag = false; continue; } if (v[p][q] == h) continue; if (g[p][q] == h) ret++; v[p][q] = h; que.push(point(p, q)); } } //printf("%d %d %d %d\n", x, y, g[x][y], ret); if (flag == false) return 0; return ret;}int solve () { int ret = 0; for (int i = 0; i < vec.size(); i++) { int x = vec[i].x, y = vec[i].y; if (v[x][y]) continue; v[x][y] = g[x][y]; ret += bfs(x, y, g[x][y]); } return ret;}int main () { int cas; scanf("%d", &cas); while (cas--) { init(); printf("%d\n", solve()); } return 0;}
2 0
- uva 12130 - Summits(BFS)
- UVA 12130 - Summits(BFS+贪心)
- UVA - 12130 Summits
- UVA12130 Summits(BFS + 贪心)
- UVALive - 3977 Summits (BFS染色)
- Hrbust 1517 Summits【Bfs+思维+贪心】好题~
- Summits poj3503
- uva 784(bfs)
- UVa 3721 - Islands 【bfs】
- uva 10603 倒水 bfs
- uva 10603 - Fill bfs
- uva 11624(BFS)
- uva 10047(BFS)
- UVA 11624 BFS Fire
- UVa Fire! 11624 (BFS)
- 双向BFS--Uva-11624
- UVA 10047 独轮车(BFS)
- uva 11624 - Fire! bfs
- Tint Color的理解和APP简单的主题化
- java web过滤器之用户登录过滤
- C#设置双屏显示模式
- VB.net遍历路径下的所有文件及所有子文件夹---递归算法
- OCP试题解析之052-- DROP PROFILE app_user
- uva 12130 - Summits(BFS)
- [Leetcode] Convert Sorted Array to Binary Search Tree
- 就是说我们的日子永远只能永远了
- 我们的感觉还是蛮好的
- 如果说我们的日子还是那么好的
- 基于UI写的简单的计算器
- C语言位移实现两个数的加法
- codeforces 459 (div 2) 题解
- ios 开发证书制作与打包上传