• 【题解】Mountain Walking-C++


    题目
    题意翻译
    题意简述:现在给一个N*N的矩阵,找一条路径从左上角走到右下角,每次可以向上下左右四个方向中某个方向走。要求走过的点中,数字最大的减去最小的。要求值越小越好。现在就是要求这个值。

    输入格式: 第一行给出一个数字N(2 <= N <= 100),代表矩阵的大小。接下来一个N行N列的矩阵,里面每个数字的值在[0,110]之间。

    输出格式: 一个数字,如翻译中所述

    题目描述
    English VietnameseFarmer John and Bessie the cow have embarked on one of those ‘active’ vacations. They spend entire days walking in the mountains and then, at the end of the day, they tire and return to their vacation cabin.

    Since climbing requires a lot of energy and they are already tired, they wish to return to the cabin using a path that has the least difference between its highest and lowest elevations, no matter how long that path is. Help FJ find this easy-to-traverse path.

    The map of the mountains is given by an N x N (2 <= N <= 100) matrix of integer elevations (0 <= any elevation <= 110) FJ and Bessie are currently at the upper left position (row 1, column 1) and the cabin is at the lower right (row N, column N). They can travel right, left, toward the top, or toward the bottom of the grid. They can not travel on a diagonal.

    输入输出格式
    输入格式:
    Line 1: The single integer, N
    Lines 2…N+1: Each line contains N integers, each of which specifies a square’s height. Line 2 contains the first (top) row of the grid; line 3 contains the second row, and so on. The first number on the line corresponds to the first (left) column of the grid, and so on.
    输出格式:
    An integer that is the minimal height difference on the optimal path.

    输入输出样例
    输入样例#1:
    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
    输出样例#1:
    2

    思路
    这道题用二分!!!
    题目描述都给的很明显了好吗!!
    n最大到100,这些值的最大值才110!
    110用来二分,绝对快啊。
    所以就二分差值,每次判断就可以了,函数都写bool类型就行了。

    代码

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 int n,l,r=110,mid;
     4 int mp[110][110];
     5 bool vis[110][110];
     6 struct node
     7 {
     8     int x,y;
     9 };
    10 int dir[4][2]={{0,1},{0,-1},{1,0},{-1,0}};
    11 bool bfs(int le,int ri)
    12 {
    13     if(mp[1][1]<le||mp[1][1]>ri)return 0;
    14     queue<node> q;
    15     q.push((node){1,1});
    16     vis[1][1]=1;
    17     while(!q.empty())
    18     {
    19         node now=q.front();
    20         q.pop();
    21         if(now.x==n&&now.y==n)return 1;
    22         for(int i=0;i<4;i++)
    23         {
    24             int tx=now.x+dir[i][0];
    25             int ty=now.y+dir[i][1];
    26             if(1<=tx&&tx<=n&&1<=ty&&ty<=n&&!vis[tx][ty])
    27             {
    28                 vis[tx][ty]=1;
    29                 if(mp[tx][ty]>=le&&mp[tx][ty]<=ri)
    30                     q.push((node){tx,ty});
    31             }
    32         }
    33     }
    34     return 0;
    35 }
    36 bool check(int d)
    37 {
    38     for(int low=0;low+d<=110;low++)
    39     {
    40         memset(vis,0,sizeof(vis));
    41         if(bfs(low,low+d))return 1;
    42     }
    43     return 0;
    44 }
    45 int main()
    46 {
    47     cin>>n;
    48     for(int i=1;i<=n;i++)
    49     {
    50         for(int j=1;j<=n;j++)
    51         {
    52             cin>>mp[i][j];
    53         }
    54     }
    55     while(l<r)
    56     {
    57         mid=(l+r)/2;
    58         if(check(mid))r=mid;
    59         else l=mid+1;
    60     }
    61     cout<<r;
    62     return 0;
    63 }
    个人博客地址: www.moyujiang.com 或 moyujiang.top
  • 相关阅读:
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 136 只出现一次的数字
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 135 分发糖果
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 134 加油站
    Java实现 LeetCode 133 克隆图
  • 原文地址:https://www.cnblogs.com/moyujiang/p/11213612.html
Copyright © 2020-2023  润新知