• 1031. 二哥在黄山


    Description

    二哥与女朋友到黄山旅行。他们在山上玩了一整天,发现天色已晚,该回家了。而突然又开始下起了雨,二哥的女朋友表示非常不爽:“都是你搞的,早知道就不和你来了。”

    二哥当然不能抛下女朋友不管,并且二哥也不想露宿在山上。于是他摊开被雨淋湿的地图。

    黄山地图是一个N*N的矩阵,矩阵中的每一项表示那个地方的高度。二哥与女朋友处在左上角,他们的住处在右下角。在矩阵中可以朝上下左右走,但不能沿着对角线行走。二哥的女朋友不喜欢颠簸,所以二哥需要找到一条回到住处的路径,使得路径上的最高点与最低点之差尽量小,而不需要管这条路径有多长。

    Input Format

    第一行:N 接下来N行 N*N的整数矩阵,(0110

    )。 (2N100

    )

    Output Format

    一个整数,表示颠簸最小的路径中最高点与最低点的高度差。

    Sample Input

    5
    1 1 3 6 8
    1 2 2 5 5
    4 4 0 3 3
    8 0 2 3 4
    4 3 0 2 1
    

    Sample Output

    2
    

    Source

    USACO 2003 U S Open

    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct node{
        int raw;
        int col;
    };
    
    int n,map[101][101];  //地图边长及数据
    bool bfs(int low,int);
    
    int main(){
        int i,j,high,low,max=-1,min=120;
        cin>>n;
        for (i=1;i<=n;++i)
            for (j=1;j<=n;++j) {
                cin>>map[i][j];
                max = map[i][j]>max ? map[i][j]:max;
                min = map[i][j]<min ? map[i][j]:min;
            }
        high = max - min;
        low = 0;
        while (high>low+1) {
            int mid = (high+low)/2;
            bool flag = 0;
            for (i=min;i<=max-mid;++i) {
                if (bfs(i,i+mid)) {
                    flag = true;
                    break;
                }
            }
            if (flag) high = mid;
            else low = mid;
        }
        cout<<high;
        return 0;
    }
    bool bfs(int low,int high) {
        int to_x[4] = {-1,1,0,0},to_y[4] = {0,0,-1,1};  //便于扫描四周点
        bool state[101][101] = {0}; //判断是否已经扫描
        queue<node>open;  //待扫描队列
        if (map[1][1]<low || map[1][1]>high ||
            map[n][n]<low || map[n][n]>high) return false;
        node open_in,open_out;
        open_in.col = 1;
        open_in.raw = 1;
        open.push(open_in);
        state[1][1] = 1;
        while (open.size()>0) {
            open_out = open.front();
            open.pop();
            for (int i = 0;i < 4;++i) {  //向四周扫描
                int x = open_out.raw + to_x[i];
                int y = open_out.col + to_y[i];
                if (x<1 || x>n || y<1 || y>n) continue;
                if (map[x][y] >= low && map[x][y] <= high && state[x][y]==0) {
                    if (x==n && y==n) return true;
                    open_in.col = y;
                    open_in.raw = x;
                    open.push(open_in);
                }
                state[x][y] = 1;
            }
        }
        return false;
    }
  • 相关阅读:
    (十三)子查询
    (十二)多表查询
    MFC读写配置ini文件
    (十一)分组函数(多行函数)
    Django(二十一)组合搜索
    Django(二十)model中的 class Meta
    (十)单行函数
    (九)逻辑运算,order by,desc
    类作为成员变量
    内部类——匿名内部类
  • 原文地址:https://www.cnblogs.com/bernieloveslife/p/7966900.html
Copyright © 2020-2023  润新知