首页 > ACM题库 > HDU-杭电 > hdu 2339 Summits[解题报告]C++
2014
01-05

hdu 2339 Summits[解题报告]C++

Summits

问题描述 :

You recently started working for the largest map drawing company in the Netherlands. Part of your job is to determine what the summits in a particular landscape are. Unfortunately, it is not so easy to determine which points are summits and which are not, because we do not want to call a small hump a summit. For example look at the landscape given by the sample input.

We call the points of height 3 summits, since there are no higher points. But although the points of height 2, which are to the left of the summit of height 3, are all higher than or equal to their immediate neighbours, we do not want to call them summits, because we can reach a higher point from them without going to low (the summits of height 3). In contrast, we do want to call the area of height 2 on the right a summit, since if we would want to walk to the summit of height 3, we first have to descend to a point with height 0.

After the above example, we introduce the concept of a d-summit. A point, with height h, is a d-summit if and only if it is impossible to reach a higher point without going through an area with height smaller than or equal to h-d.

The problem is, given a rectangular grid of integer heights and an integer d, to find the number of d-summits.

Input

输入:

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with three integers 1 ≤ h ≤ 500, 1 ≤ w ≤ 500 and 1 ≤ d ≤ 1 000 000 000. h and w are the dimensions of the map. d is as defined in the text.

h lines with w integers, where the xth integer on the yth line denotes the height 0 ≤ h ≤ 1 000 000 000 of the point (x, y).

输出:

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with three integers 1 ≤ h ≤ 500, 1 ≤ w ≤ 500 and 1 ≤ d ≤ 1 000 000 000. h and w are the dimensions of the map. d is as defined in the text.

h lines with w integers, where the xth integer on the yth line denotes the height 0 ≤ h ≤ 1 000 000 000 of the point (x, y).

样例输入:

1
6 10 2
0 0 0 0 0 0 0 0 0 0
0 1 2 1 1 1 1 0 1 0
0 2 1 2 1 3 1 0 0 0
0 1 2 1 3 3 1 1 0 0
0 2 1 2 1 1 1 0 2 0
0 0 0 0 0 0 0 0 0 0

样例输出:

4


这道题的数据量很大,基本上除了O(w*h)的复杂度以外都是不能接受的,然而这样的复杂度下能想到的就

是枚举或者广搜了,而枚举的话有种神话的感觉,所以只有广搜可以考虑了。
有一个非常有用的性质:如果点A可以达到点B,点C也可以到达点B,且点A大于点C,则点C也可到

达点A。这样的思路下即可做广搜了,从最大的值开始,广搜出其可到达的所有点,如果遇到一个已被较

大点搜过的点,则记录该点不是所求点。否则该点能访问到的所有与该点等值的点都是所求点。
核心代码如下:

int bfs()
{
int i,tot=0,tt,sg;
for (i=1;i<=h;++i)
   memset(vst[i],-1,(w+2)*sizeof(vst[i][0]));
for (i=m;i;)
{
   --i;
   if (vst[sts[i].x][sts[i].y]<0)
   {
    hd=0;
    tl=1;
    qx[hd]=sts[i].x;
    qy[hd]=sts[i].y;
    vst[sts[i].x][sts[i].y]=i;
    int ax,ay,bx,by,lb=sts[i].h-d,j;
    tt=0;
    sg=1;
    while (hd<tl)
    {
     ax=qx[hd];
     ay=qy[hd];
     ++hd;
     if (grids[ax][ay]==grids[sts[i].x][sts[i].y]) ++tt;
     for (j=0;j<4;++j)
     {
      bx=ax+cx[j];
      by=ay+cy[j];
      if (isvalid(bx,by)&&grids[bx][by]>lb)
      {
       if (vst[bx][by]>i) sg=0;
       if (vst[bx][by]<0)
       {
        vst[bx][by]=i;
        qx[tl]=bx;
        qy[tl]=by;
        ++tl;
       }
      }
     }
    }
    if (sg) tot+=tt;
   }
}
return tot;
}

 


  1. 有一点问题。。后面动态规划的程序中
    int dp[n+1][W+1];
    会报错 提示表达式必须含有常量值。该怎么修改呢。。

  2. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  3. I go through some of your put up and I uncovered a good deal of expertise from it. Many thanks for posting this sort of exciting posts