USACO Tractor

洛谷 P3073 [USACO13FEB]拖拉机Tractor

洛谷传送门

JDOJ 2351: USACO 2013 Feb Silver 2.Tractor

JDOJ传送门

题意翻译

题目描述

FJ有块农田太崎岖了,他要买一辆新拖拉机才能在这里巡视。这块农田由N x N个格子的非负整数表示高度(1<=N<=500)。拖拉机从当前格子走到相邻格子(东、南、西、北四个方向)的代价为高度差D,则FJ驶过这两个格子的拖拉机最少也要值D块钱。

FJ愿意花足够的钱买一辆新的拖拉机使得他能以最小的高度差走遍所有格子的一半(如果格子总数是奇数,那么一半的值为四舍五入的值)。因为FJ很懒,所以他找到你帮他编程计算他最小需要花多少钱买到符合这些要求的拖拉机。

输入输出格式

输入格式:

第一行为一个整数N

第2到N+1行每行包含N个非负整数(不超过1,000,000),表示当前格子的高度。

输出格式:

共一行,表示FJ买拖拉机要花的最小价钱。

题目描述

One of Farmer John's fields is particularly hilly, and he wants to purchase a new tractor to drive around on it. The field is described by an N x N grid of non-negative integer elevations (1 <= N <= 500). A tractor capable of moving from one grid cell to an adjacent cell (one step north, east, south, or west) of height difference D costs exactly D units of money.

FJ would like to pay enough for his tractor so that, starting from some grid cell in his field, he can successfully drive the tractor around to visit at least half the grid cells in the field (if the number of total cells in the field is odd, he wants to visit at least half the cells rounded up). Please help him compute the minimum cost necessary for buying a tractor capable of this task.

输入格式

* Line 1: The value of N.

* Lines 2..1+N: Each line contains N space-separated non-negative integers (each at most 1 million) specifying a row of FJ's field.

输出格式

* Line 1: The minimum cost of a tractor that is capable of driving around at least half of FJ's field.

样例输入

5 0 0 0 3 3 0 0 0 0 3 0 9 9 3 3 9 9 9 3 3 9 9 9 9 3

样例输出

3

思路:深搜+二分。

应该挺好想的,二分的两个边界分别为输入矩阵的最大值和最小值。

然后进入二分程序,判断函数就是当前花费下判断能不能达到这个矩阵格数的一半。

这里调用DFS,DFS的思路是这样的。

我们搜索的时候搜的是整个矩阵,所以建立方向数组和标记数组。

我们的DFS有返回值,这个返回值是表示在k高度下能够到达的格数。

那么函数就很好写了,一开始的时候res一定要置成一!!!

然后从4个方向开始搜索,判断的条件是不越界+没搜过+差值能够承受。

然后就可以累加了。

累加之后就可以在判断函数里用了。

用完就A了

对了!!!

注意二分的更新边界值,也就是出口,我一开始死里面了!

CODE:

#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int n,ans,l=1e9,r=-1;
int map[501][501],v[501][501];
int dx[]={-1,1,0,0};
int dy[]={0,0,1,-1};
int dfs(int x,int y,int k)
{
    v[x][y]=1;
    int res=1;
    for(int i=0;i<4;i++)
    {
        int xx=x+dx[i];
        int yy=y+dy[i];
        if(xx>0 && yy>0 && xx<=n && yy<=n && v[xx][yy]==0 && abs(map[xx][yy]-map[x][y])<=k)
            res+=dfs(xx,yy,k);
    }
    return res;
}
bool check(int k)
{
    memset(v,0,sizeof(v));
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            if(dfs(i,j,k)>=n*n/2)
                return 1;
    return 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        {
            scanf("%d",&map[i][j]);
            l=min(l,map[i][j]);
            r=max(r,map[i][j]);
        }
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(check(mid))
            ans=mid,r=mid-1;
        else
            l=mid+1;
    }
    printf("%d",ans);
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/fusiwei/p/11264505.html