20150301题解

山峰和山谷(grz)
Description
Byteasar特别喜欢爬山,在爬山的时候他就在研究山峰和山谷。为了能够让他对他的旅程有一个安排,他想知道山峰和山谷的数量。
给定一个地图,为Byteasar想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的。
若两个格子有公共顶点,那么他们就是相邻的格子。(所以与(i,j)相邻的格子有(i−1, j−1),(i−1,j), (i−1,j+1), (i,j−1), (i,j+1), (i+1,j−1), (i+1,j), (i+1,j+1))。
我们定义一个格子的集合S为山峰(山谷)当且仅当:
1.S的所有格子都有相同的高度。
2.S的所有格子都联通
3.对于s属于S,与s相邻的s’不属于S。都有ws>ws’(山峰),或者ws小于ws’(山谷)
你的任务是,对于给定的地图,求出山峰和山谷的数量,如果所有格子都有相同的高度,那么整个地图即是山峰,又是山谷。
Input Format
输入文件grz.in第一行包含一个正整数n,表示地图的大小(1<=n<=1000)。接下来一个n*n的矩阵,表示地图上每个格子的高度。(0<=w<=1000000000)
Output Format
输出文件grz.out应包含两个数,分别表示山峰和山谷的数量。
Sample Input 1
5
8 8 8 7 7
7 7 8 8 7
7 7 7 7 7
7 8 8 7 8
7 8 8 8 8
Sample Output 2
2 1
Sample Input 2
5
5 7 8 3 1
5 5 7 6 6
6 6 6 2 8
5 7 2 5 8
7 1 0 1 7
Sample Output 2
3 3
Solution
一道经典的bfs问题,遍历整个图求各个高度相同的连通块,若在扩展中遇到了高度比当前连通块小的,则当前连通块不是山谷;若在扩展中遇到了高度比当前连通块大的,则当前连通块不是山峰。
Code

#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
int n, map[1001][1001];
bool vis[1001][1001];
int hans, lans;//山峰、山谷 
int dx[9] = {
  0, 1, 1, 1, 0, -1, -1, -1, 0}, dy[9] = {
  0, -1, 0, 1, 1, 1, 0, -1, -1};//增量数组 
struct pnt {
    int x, y, h;        //坐标,高度 
    pnt(int x = 0, int y = 0, int h = 0) 
        :x(x), y(y), h(h) {}//构造器 
};
inline void bfs(int x, int y) {
    queue <pnt> q;
    q.push(pnt(x, y, map[x][y]));
    pnt u;
    bool hgh = true, lw = true;//记录是否为山峰、山谷 
    while(!q.empty()) {
        u = q.front();
        q.pop();
        for(int i = 1; i <= 8; ++i) {
            u.x += dx[i]; u.y += dy[i];
            if(u.x>0 && u.x<=n && u.y>0 && u.y<=n) {
                if(map[u.x][u.y] > u.h) hgh = false;
                else if(map[u.x][u.y] < u.h) lw = false;
                else if(!vis[u.x][u.y]) {
                    vis[u.x][u.y] = true;
                    q.push(pnt(u.x, u.y, map[u.x][u.y]));
                }
            }
            u.x -= dx[i]; u.y -= dy[i];
        }
    }
    if(hgh) ++hans;
    if(lw) ++lans;
}
int main() {
    freopen("grz.in", "r", stdin);
    freopen("grz.out", "w", stdout);
    scanf("%d", &n);
    for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j)
        scanf("%d", &map[i][j]);
    for(int i = 1; i <= n; ++i) for(int j = 1; j <= n; ++j)
        if(!vis[i][j]) bfs(i, j);
    printf("%d %d", hans, lans);
    return 0;
}

超级马(sup)
Description
在一个无限的棋盘上有一个超级马,它可以完成各种动作。每一种动作都是通过两个整数来确定——第一个数说明列的数(正数向右,负数向左),第二个数说明行的数(正数向上,负数向下),移动马来完成这个动作。
编写一个程序,从文本文件SUP.I

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
06-01
这道题是一道典型的费用限制最短路题目,可以使用 Dijkstra 算法或者 SPFA 算法来解决。 具体思路如下: 1. 首先,我们需要读入输入数据。输入数据中包含了道路的数量、起点和终点,以及每条道路的起点、终点、长度和限制费用。 2. 接着,我们需要使用邻接表或邻接矩阵来存储图的信息。对于每条道路,我们可以将其起点和终点作为一个有向边的起点和终点,长度作为边权,限制费用作为边权的上界。 3. 然后,我们可以使用 Dijkstra 算法或 SPFA 算法求解从起点到终点的最短路径。在这个过程中,我们需要记录到每个点的最小费用和最小长度,以及更新每条边的最小费用和最小长度。 4. 最后,我们输出从起点到终点的最短路径长度即可。 需要注意的是,在使用 Dijkstra 算法或 SPFA 算法时,需要对每个点的最小费用和最小长度进行松弛操作。具体来说,当我们从一个点 u 经过一条边 (u,v) 到达另一个点 v 时,如果新的费用和长度比原来的小,则需要更新到达 v 的最小费用和最小长度,并将 v 加入到优先队列(Dijkstra 算法)或队列(SPFA 算法)中。 此外,还需要注意处理边权为 0 或负数的情况,以及处理无法到达终点的情况。 代码实现可以参考以下样例代码: ```c++ #include <cstdio> #include <cstring> #include <queue> #include <vector> using namespace std; const int MAXN = 1005, MAXM = 20005, INF = 0x3f3f3f3f; int n, m, s, t, cnt; int head[MAXN], dis[MAXN], vis[MAXN]; struct Edge { int v, w, c, nxt; } e[MAXM]; void addEdge(int u, int v, int w, int c) { e[++cnt].v = v, e[cnt].w = w, e[cnt].c = c, e[cnt].nxt = head[u], head[u] = cnt; } void dijkstra() { priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q; memset(dis, 0x3f, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[s] = 0; q.push(make_pair(0, s)); while (!q.empty()) { int u = q.top().second; q.pop(); if (vis[u]) continue; vis[u] = 1; for (int i = head[u]; i != -1; i = e[i].nxt) { int v = e[i].v, w = e[i].w, c = e[i].c; if (dis[u] + w < dis[v] && c >= dis[u] + w) { dis[v] = dis[u] + w; q.push(make_pair(dis[v], v)); } } } } int main() { memset(head, -1, sizeof(head)); scanf("%d %d %d %d", &n, &m, &s, &t); for (int i = 1; i <= m; i++) { int u, v, w, c; scanf("%d %d %d %d", &u, &v, &w, &c); addEdge(u, v, w, c); addEdge(v, u, w, c); } dijkstra(); if (dis[t] == INF) printf("-1\n"); else printf("%d\n", dis[t]); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值