poj 2375

poj 2375这道题是一道gu

大家好,又见面了,我是你们的朋友全栈君。

        这道题是一道关于强连通分量的题目,不过这道题给出的图比较特殊,所以在求强连通分量时,可以采用广搜来做。

        这道强连通分量的题,给出的图十分特殊,如果在上下、左右四个方向相邻的区域,如果高度相同,则是相互可达的,所以我们可以通过搜索找出强连通分量,可以降低时间复杂度。

        不过在做这道时,开始想通过一次搜索来完成所有强连通分量的标记,不过有些问题一直无法解决,无奈只好多次广搜,每次找一个强连通分量。找到强连通分量,接下来的做法就和poj 1236(http://blog.csdn.net/u011008379/article/details/37995979)中的第二问做法一样了,这里就不多解释。

        代码(C++):

#include <cstdlib>
#include <iostream>
#include <algorithm>

#define MAX 509
using namespace std;

//#define LOCAL

typedef pair<int,int> pii;

int map[MAX][MAX],dir[][2]={
  
  {0,-1},{1,0},{0,1},{-1,0}},id[MAX][MAX],vis[MAX][MAX],odeg[MAX*MAX],ideg[MAX*MAX];
int p,s,n,m;
pii queue[MAX*MAX];

void bfs(int u,int v,int c)
{
     int a,b,i;
     pii tmp;
     
     p=s=0;
     id[u][v]=c;
     queue[p++]=make_pair(u,v);
     vis[u][v]=true; 
        
     while(s<p)
     {
         tmp=queue[s++];
         for(i=0;i<4;i++)
         {
             a=dir[i][1]+tmp.first;
             b=dir[i][0]+tmp.second;
                                  
             if(a>=0&&a<n&&b>=0&&b<m&&map[a][b]==map[tmp.first][tmp.second])
             {
                   if(vis[a][b]) continue;
                   id[a][b]=c;                               
                   queue[p++]=make_pair(a,b);  
                   vis[a][b]=true;                 
             }            
         }                    
     }
}

int main(int argc, char *argv[])
{
#ifdef LOCAL
   freopen("in.txt","r",stdin);
   freopen("out.txt","w",stdout);
#endif
    int i,j,k,x,y,a,b,c;
    while(scanf("%d %d",&m,&n)!=EOF)
    {
         for(i=0;i<n;i++)
         {
            for(j=0;j<m;j++)
            {
                scanf("%d",&map[i][j]);            
            }             
         }
         
         c=0;                 
         memset(vis,false,sizeof(vis));
         memset(id,-1,sizeof(id));
         for(i=0;i<n;i++)
         {
             for(j=0;j<m;j++)
             {
                 if(!vis[i][j]) bfs(i,j,c++);            
             }            
         }
         if(c==1)
         {
             printf("0\n");    
         }else{
             memset(ideg,0,sizeof(ideg));
             memset(odeg,0,sizeof(odeg));
             for(i=0;i<n;i++)
             {
                 for(j=0;j<m;j++)
                 {
                     for(k=0;k<4;k++)
                     {
                        a=dir[k][1]+i;
                        b=dir[k][0]+j;
                        if(a>=0&&a<n&&b>=0&&b<m)
                        {
                             if(id[i][j]!=id[a][b]) 
                             {
                                 if(map[i][j]>map[a][b])
                                 {
                                     odeg[id[i][j]]++;
                                     ideg[id[a][b]]++;                    
                                 }else{
                                    odeg[id[a][b]]++;
                                    ideg[id[i][j]]++;  
                                 }                   
                             }                    
                        }            
                     }           
                 }             
             }
             x=y=0;
             for(i=0;i<c;i++)
             {
                if(odeg[i]==0) x++;
                if(ideg[i]==0) y++;            
             }
             printf("%d\n",max(x,y));
         }          
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}

题目(
http://poj.org/problem?id=2375):

Cow Ski Area
Time Limit: 1000MS   Memory Limit: 65536K
     

Description

Farmer John’s cousin, Farmer Ron, who lives in the mountains of Colorado, has recently taught his cows to ski. Unfortunately, his cows are somewhat timid and are afraid to ski among crowds of people at the local resorts, so FR has decided to construct his own private ski area behind his farm. 

FR’s ski area is a rectangle of width W and length L of ‘land squares’ (1 <= W <= 500; 1 <= L <= 500). Each land square is an integral height H above sea level (0 <= H <= 9,999). Cows can ski horizontally and vertically between any two adjacent land squares, but never diagonally. Cows can ski from a higher square to a lower square but not the other way and they can ski either direction between two adjacent squares of the same height. 

FR wants to build his ski area so that his cows can travel between any two squares by a combination of skiing (as described above) and ski lifts. A ski lift can be built between any two squares of the ski area, regardless of height. Ski lifts are bidirectional. Ski lifts can cross over each other since they can be built at varying heights above the ground, and multiple ski lifts can begin or end at the same square. Since ski lifts are expensive to build, FR wants to minimize the number of ski lifts he has to build to allow his cows to travel between all squares of his ski area. 

Find the minimum number of ski lifts required to ensure the cows can travel from any square to any other square via a combination of skiing and lifts.

Input

* Line 1: Two space-separated integers: W and L 

* Lines 2..L+1: L lines, each with W space-separated integers corresponding to the height of each square of land.

Output

* Line 1: A single integer equal to the minimal number of ski lifts FR needs to build to ensure that his cows can travel from any square to any other square via a combination of skiing and ski lifts

Sample Input

9 3
1 1 1 2 2 2 1 1 1
1 2 1 2 3 2 1 2 1
1 1 1 2 2 2 1 1 1

Sample Output

3

Hint

This problem has huge input data,use scanf() instead of cin to read data to avoid time limit exceed. 

OUTPUT DETAILS: 

FR builds the three lifts. Using (1, 1) as the lower-left corner, 

the lifts are (3, 1) <-> (8, 2), (7, 3) <-> (5, 2), and (1, 3) <-> 

(2, 2). All locations are now connected. For example, a cow wishing 

to travel from (9, 1) to (2, 2) would ski (9, 1) -> (8, 1) -> (7, 

1) -> (7, 2) -> (7, 3), take the lift from (7, 3) -> (5, 2), ski 

(5, 2) -> (4, 2) -> (3, 2) -> (3, 3) -> (2, 3) -> (1, 3), and then 

take the lift from (1, 3) – > (2, 2). There is no solution using 

fewer than three lifts.

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130775.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Js二代身份证号码正则验证

    Js二代身份证号码正则验证一代身份证号码是十五位,2013年1月1日开始,咱们中国全面停止使用一代身份证了。二代身份证号码:1-6位:表示行政区划的代码。1、2位,所在省(直辖市,自治区)代码;3、4位,所在地级市(自治州)代码;5、6位,所在区(县,自治县,县级市)的代码;7-14位:表示出生年、月、日15-16位:所在地派出所代码17位:性别。奇数(1、3、5、7、9)男性,偶数(2、4、…

    2022年6月27日
    29
  • 已成功刷新dns解析缓存后怎么操作_刷新dns缓存的命令

    已成功刷新dns解析缓存后怎么操作_刷新dns缓存的命令步骤一、首先按住键盘win+R组合键,打开了一个运行窗口,之后在运行窗口上输入“CMD”命令,执行该命令即可打开命令提示符窗口了。步骤二、然后在命令提示符上线查看下你的电脑上的dns缓存的全部信息,输入“ipconfig/displaydns”即可查询dns缓存信息了。之后在输入“ipconfig/flushdns”命令敲回车键即可将你本机上的dns缓存清空了。当然如果你不信的话,可以重新输入“ipconfig/displaydns”查询dnd缓存就能知道是否清空了本机dns缓存信息了。运行:ip

    2025年5月22日
    3
  • Java获取二维数组行列长度「建议收藏」

    Java获取二维数组行列长度「建议收藏」二维数组intarray[][]=newint[3][3];行长度:array.length列长度:array[i].lengthclassTest{for(inti=0;i

    2022年5月12日
    51
  • vue响应式原理的实现

    vue响应式原理的实现Vue最独特的特性之一,是其非侵入性的响应式系统。数据模型仅仅是普通的JavaScript对象。而当你修改它们时,视图会进行更新。这使得状态管理非常简单直接,不过理解其工作原理同样重要,这样你可以避开一些常见的问题。—-官方文档引言Vue的数据双向绑定,响应式原理,其实就是通过Object.defineProperty()结合发布者订阅者模式来实现的。Observer通过O…

    2022年5月1日
    42
  • Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法

    Macbook pro/air 2013 late -2014 使用转接卡更换NVME SSD休眠不醒问题的解决办法、、1.手上512GMBP2013late差不多满了,因为穷,所以在淘宝上买了一个NVME转Macbookpcie,然后再买一个NVME2T的硬盘2.NVME因为需要最新的FirmwareRom支持,所以必须使用原装的硬盘(必须原装)安装Mac14以上,我安装了14.5.要不然识别不出来新安装的NVME硬盘3.买之前就知道是会有休眠问题的,问了卖家推荐了一些型号说不…

    2022年6月23日
    46
  • SMO算法最通俗易懂的解释

    SMO算法最通俗易懂的解释我的机器学习教程「美团」算法工程师带你入门机器学习已经开始更新了,欢迎大家订阅~任何关于算法、编程、AI行业知识或博客内容的问题,可以随时扫码关注公众号「图灵的猫」,加入”学习小组“,沙雕博主在线答疑~此外,公众号内还有更多AI、算法、编程和大数据知识分享,以及免费的SSR节点和学习资料。其他平台(知乎/B站)也是同名「图灵的猫」,不要迷路哦~SVM通常用对偶问题来求解,这…

    2022年6月30日
    27

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号