P2658 Rally game

Title Description

Boyi City will hold a rally race.

Uneven track, it is described as a grid of M * N represents the altitude (1≤ M, N ≤500), the range of elevation of each cell between 0 and 10 ^ 9.

Some of the cells is defined as a road sign. Organizers want to specify a degree of difficulty of the entire route D, so that players from any signpost to an adjacent cell on a path through which other signs of no greater than the difference in altitude D. That is the degree of difficulty of D refers to the minimum guarantee each other up all the road signs. And any cell in which four cells on the East and West directions are adjacent.

Input Format

The first line of two integers M and N. Lines 2 to M + 1 lines, each of the N described integer altitude. Second row to the 2 + M 1 + 2M

Lines of N integers, each number is either 0 or 1 indicates that the cell is a road sign.

Output Format

An integer that degree of difficulty of the track D.

Sample input and output

Input # 1
3 5 
20 21 18 99 5  
19 22 20 16 26
18 17 40 60 80
1 0 0 0 1
0 0 0 0 0
0 0 0 0 1
Output # 1
21 


At first glance I thought it was one of the most short-circuit, later found seems to BFS (looks like someone with a disjoint-set)


dichotomous answers + BFS;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define maxn 5555
using namespace std;

int n,m,maze[maxn][maxn],cnt,kk[maxn][maxn],startx,starty;

bool vis[maxn][maxn];

struct node{
    int x,y;
}a[maxn][maxn];

const int dx[4]={1,-1,0,0};

const int dy[4]={0,0,1,-1};

int abss(int a,int b){
    if(a>b) return a-b;
    else return b-a;
}

bool check(int s)
{
    queue<node> q;
    memset(vis,false,sizeof(vis));
    vis[startx][starty]=true;
    q.push(a[startx][starty]);
    int sum=1;
    while(!q.empty())
    {
        node cur=q.front();
        q.pop();
        for(int k=0;k<4;k++)
        {
            int x1=cur.x+dx[k];
            int y1=cur.y+dy[k];
            if(x1<1||x1>m||y1<1||y1>n||vis[x1][y1]||abss(maze[x1][y1],maze[cur.x][cur.y])>s){
                continue;
            }
            vis[x1][y1]=true;
            sum+=kk[x1][y1];
            q.push(a[x1][y1]);
            if(sum==cnt){
                return true;
            }

        }
    }
    return false;
}

int main()
{
    scanf("%d%d",&m,&n);
    int low=0,high=0,mid,ans;
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&maze[i][j]);
            a[i][j].x=i;
            a[i][j].y=j;
            high=max(high,maze[i][j]);
        }
    }
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&kk[i][j]);
            if(kk[i][j])
            {
                cnt++;
                if(cnt==1)
                {
                    startx=i;
                    starty=j;
                }
            }
        }
    }
    while(low<=high)
    {
        mid=(low+high)/2;
        if(check(mid)){
            ans=mid;
            high=mid-1;
        }
        else low=mid+1;
    }
    printf("%d",ans);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/hrj1/p/11723616.html