L - Farm Irrigation (并查集

给你一个管道地图,求联通块个数

 

很简单的并查集,简单预处理输入一下

 

但zoj能过,hdu就是莫名其妙的wa

后来发现是我地图的表示时边界的处理有问题

 

例如

ABD

KJH

KNG

我把这个矩阵读取成一行:ABDKJHKNG

对于第i个元素 上下左右就表示为 ut=i - n ,lt=i -1 ,rt=i+1 ,dt=i+n;

判断边界:  ut>0   i%n !=1   i%n !=0  dt<=m*n

这样平常没什么问题,所以zoj能过

但是。。。 当矩阵宽度为1时,左右边界就会合并,这时判断 左边界 i%n !=1 就会出错,因为此时“余1” 就是 余0

所以要特判一下1.。。。

以后这种情况要特别注意(不过再碰到题我可能就不这样表示上下左右了)

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>

using namespace std;

int pre[500000];
int node[500000];
const int u[50 ]={1,1,0,0,1,0,1,1,0,1,1};
const int l [50 ]={1,0,1,0,0,1,1,1,1,0,1};
const int r [50 ]={0,1,0,1,0,1,1,0,1,1,1};
const int d[50 ]={0,0,1,1,1,0,0,1,1,1,1};

void init( int n){
     for( int i=0 ;i<=n;i++)
        pre[i] = i;
}

int find( int x){

     int r= x;
     while( pre[x] != x){
        x= pre[x];
     }
     int t;
     while( r!=x){
        t = pre[r];
        pre[r] = x;
        r = t;
     }
     return x;
}

void add( int a ,int b){
   // cout<<a <<' '<<b<<endl;
    int x=find(a);
    int y=find(b);
    if( x!=y) pre[x] =y;
    return ;
}

int main( ){
     int m ,n;
     while( cin>>m>>n){
            if( m<0 || n<0) break;
         int an = m*n;
         init( an);
         string op;
         int cnt = 1;
         for( int i=1 ;i<=m ;i++){
            cin>> op;
            for( int j=0 ; j<n ;j++)
                node[cnt++] =(int)(op[j]-'A');
         }

         for( int i=1 ;i<=an ;i++){
             int t=node[i];
             int ut=i - n ,lt=i -1 ,rt=i+1 ,dt=i+n;
             if( u[ t] && ut>0 && d[ node[ut] ])add(i ,ut);
             if( l[ t] && i%n !=1 && n !=1 && r[ node[lt] ])add(i ,lt); //就是这一行
             if( r[ t] && i%n !=0  && n !=1&& l[ node[rt] ])add(i ,rt);
             if( d[ t] && dt<=an && u[ node[dt] ])add(i ,dt);
         }
         int ans=0;
         for( int i=1 ; i<=an ;i++){
            if( pre[i] == i)ans++;
          //  cout<<i<<' '<<pre[i]<<endl;
         }
         printf("%d\n" ,ans);
     }
     return 0;
}

 

转载于:https://www.cnblogs.com/-ifrush/p/10660433.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用AquaCrop-OSPy模拟水稻灌溉的Python代码: ```python import os import sys # 添加 AquaCrop-OSPy 路径 sys.path.append(os.path.join(os.getcwd(), 'AquaCrop-OSPy')) from AquaCropOS import * from AquaCropOS.Outputs import * from AquaCropOS.Utils import * from AquaCropOS.Plant import * from AquaCropOS.Soil import * # 创建 AquaCrop-OS 模型 model = Model() # 定义模型输入参数 model.Input.StartDate = '01/01/2009' model.Input.EndDate = '12/31/2009' model.Input.Latitude = 40.0 model.Input.Longitude = -120.0 model.Input.Altitude = 100.0 model.Input.Soil.ThetaS = 0.42 model.Input.Soil.ThetaFC = 0.35 model.Input.Soil.ThetaPWP = 0.15 model.Input.Soil.Ksat = 50.0 model.Input.Soil.SoilDepth = 1.5 model.Input.Soil.RootingDepth = 0.5 model.Input.Soil.BulkDensity = 1.35 model.Input.Irrigation.Type = 'Full' model.Input.Irrigation.Stages = 'Sowing, Emergence, Mid-season, Late-season' model.Input.Crop.Name = 'Rice' model.Input.Crop.PlantingDate = '06/01/2009' model.Input.Crop.HarvestDate = '10/01/2009' model.Input.Crop.PlantingDensity = 250.0 model.Input.Crop.MaxRootingDepth = 0.5 model.Input.Crop.RowSpacing = 0.2 model.Input.Crop.SeedDensity = 200.0 model.Input.Crop.SeedWeight = 0.02 model.Input.Crop.HIstart = 0.4 model.Input.Crop.HIend = 0.9 model.Input.Crop.HIadj = 1.0 model.Input.Crop.CCx = 0.35 model.Input.Crop.Kcb = [0.25, 0.70, 1.15, 1.15, 1.05, 0.90, 0.70, 0.50, 0.35, 0.35, 0.35, 0.35] model.Input.Crop.PolResistance = 0.0 model.Input.Crop.MaturitySignificant = True model.Input.Crop.CropSeason = 'Monsoon' # 运行模型 model.run() # 输出结果 Results = Output(model) print('Total yield: {0} kg/ha'.format(Results.TotalYield)) print('Total irrigation: {0} mm'.format(Results.TotalIrrigation)) ``` 此代码使用 AquaCrop-OSPy 模块创建了 AquaCrop-OS 模型,并定义了模型的输入参数,包括日期、纬度、经度、高度、土壤性质、灌溉类型、作物类型和种植信息等。然后,模型被运行,并使用 AquaCrop-OSPy 模块的输出功能输出了模拟结果,包括总产量和总灌溉量。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值