1. 程式人生 > >【POJ】2110Mountain Walking(二分查詢+BFS)

【POJ】2110Mountain Walking(二分查詢+BFS)

Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 4982   Accepted: 1619

Description

Farmer 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. 

Input

* 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. 

Output

* Line 1: An integer that is the minimal height difference on the optimal path. 

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

 

題目大意:給你一個N*N的網格,並給出網格中每個點的海拔高度,現在要你找出從左上角到右下角那個點的一條路,且這條路所有點中,最高海拔-最低海拔之差最小。輸出該最小值。

思路:從L=0,R=100開始二分查詢【low,up】 區間內高度值,用BFS查詢判斷是否存在這樣的路徑,

路徑從左上角到右下角。。。

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<queue>
#include<algorithm>
#define maxn 105
using namespace std;

int map[maxn][maxn];
int vis[maxn][maxn];
int n;
int dr[]={1,0,-1,0};
int dc[]={0,1,0,-1};

struct Node
{
    int r,c;
    Node(int r,int c):r(r),c(c){}
};

bool BFS(int low,int up)
{
    if(map[1][1]<low||map[1][1]>up) return false;

    queue<Node> Q;
    Q.push(Node(1,1));
    vis[1][1]=0;
    while(!Q.empty())
    {
        Node node=Q.front(); Q.pop();
        int r=node.r,c=node.c;
        for(int i=0;i<4;i++)
        {
            int nr=r+dr[i];
            int nc=c+dc[i];

            if(nr>=1&&nr<=n&&nc>=1&&nc<=n&&vis[nr][nc]==0)
            {
                vis[nr][nc]=1;
                if(map[nr][nc]>=low&&map[nr][nc]<=up)
                {
                    Q.push(Node(nr,nc));
                    if(nr==n&&nc==n) return true;
                }
            }
        }
    }
    return false;
}

bool check(int d)
{
    for(int low=0;low+d<=110;low++)
    {
        memset(vis,0,sizeof(vis));
        if(BFS(low,low+d)) return true;
    }
    return false;
}


main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            scanf("%d",&map[i][j]);
    int L=0,R=110;
    while(L<R)
    {
        int mid=(L+R)/2;
        if(check(mid)) R=mid;
        else
            L=mid+1;
    }
    printf("%d\n",R);
    return 0;
}