Google Code Jam 2009预选赛第二题----Watersheds

ObservableList 普通的List是“数据容器”,而是“数据源 + 广播电台”。添加的功能:监听器、精准变更报告、原子替换、实时过滤/排序视图。本质区别普通List是被动的(你问它才给)。是主动的(它变了就立刻告诉你,并且告诉你“哪里变了、怎么变的”)。正是因为它具备了这种“主动广播”的能力,JavaFX 才能实现数据驱动 UI的响应式编程模型。你之前在ComboBox源码中看到的监听器,正是依赖的机制才得以生效的。 阅读详情

Problem

Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.

Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.

  • From each cell, water flows down to at most one of its 4 neighboring cells.
  • For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell's, then the water does not flow, and the current cell is called a sink.
  • Otherwise, water flows from the current cell to the neighbor with the lowest altitude.
  • In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.

Every cell that drains directly or indirectly to the same sink is part of the same drainage basin. Each basin is labeled by a unique lower-case letter, in such a way that, when the rows of the map are concatenated from top to bottom, the resulting string is lexicographically smallest. (In particular, the basin of the most North-Western cell is always labeled 'a'.)

 

Input

The first line of the input file will contain the number of maps, T. T maps will follow, each starting with two integers on a line -- H and W -- the height and width of the map, in cells. The next H lines will each contain a row of the map, from north to south, each containing W integers, from west to east, specifying the altitudes of the cells.

Output

For each test case, output 1+H lines. The first line must be of the form

Case #X:

where X is the test case number, starting from 1. The next H lines must list the basin labels for each of the cells, in the same order as they appear in the input.

 

Limits

T ≤ 100;

Small dataset

1 ≤ H, W ≤ 10;
0 ≤ altitudes < 10.
There will be at most two basins.

Large dataset

1 ≤ H, W ≤ 100;
0 ≤ altitudes < 10,000.
There will be at most 26 basins.

这个题目其实是连通块标记问题, 如果水能从P0(x0,y0)流到p1(x1, y1),就说明p0和p1是连通的.

然后把所有的连通块标记出来, 最后按照从左到右,从上到下的顺序重新填写连通块的标记号码.

这个题目我是用类似于并查集的方法来标记连通块的, 最后再对块进行排序.

由于一开始没有排序,所以递交失败了,后来加上排序后,small set成功了,但是最后,成绩出来的时候, big set的结果不对,到现在还没找到问题.....

 

上代码(big set有问题):

#include <stdio.h>
#include <malloc.h>
#include <memory.h>

int SetVal(char* pVal, int* pPar, int nPos)
{
 if(pVal[nPos]==0)
 {
  pVal[nPos] = SetVal(pVal, pPar, pPar[nPos]);
 }
 return pVal[nPos];
}

int main()
{
 FILE* fp;
 int i, x, y;
 fp = fopen("1.txt", "r");

 int nRound = 0;
 fscanf(fp, "%d", &nRound);
 
 for(i=0; i<nRound; i++)
 {
  int nWidth, nHeight;
  fscanf(fp, "%d %d", &nHeight, &nWidth);

  int* pHei = (int*)malloc(nWidth*nHeight*sizeof(int));
  int* pPar = (int*)malloc(nWidth*nHeight*sizeof(int));
  char* pVal = (char*)malloc(nWidth*nHeight*sizeof(char));
  memset(pVal, 0, nWidth*nHeight*sizeof(char));
  int* pHeiT = pHei;
  for(y=0; y<nHeight; y++)
  {
   for(x=0; x<nWidth; x++, pHeiT++)
   {
    fscanf(fp, "%d", pHeiT);
   }
  }

  int nPos = 0;
  int nChar = 0;
  for(y=0; y<nHeight; y++)
  {
   for(x=0; x<nWidth; x++, nPos++)
   {
    int nHei = pHei[nPos];
    int nMinHei = 0xFFFFFF;
    int nMinPos;
    if(y>0)
    {
     if(pHei[nPos-nWidth]<nMinHei)
     {
      nMinHei = pHei[nPos-nWidth];
      nMinPos = nPos-nWidth;
     }
    }
    if(x>0)
    {
     if(pHei[nPos-1]<nMinHei)
     {
      nMinHei = pHei[nPos-1];
      nMinPos = nPos-1;
     }
    }
    if(x<nWidth-1)
    {
     if(pHei[nPos+1]<nMinHei)
     {
      nMinHei = pHei[nPos+1];
      nMinPos = nPos+1;
     }
    }
    if(y<nHeight-1)
    {
     if(pHei[nPos+nWidth]<nMinHei)
     {
      nMinHei = pHei[nPos+nWidth];
      nMinPos = nPos+nWidth;
     }
    }

    if(nMinHei<nHei)
    {
     pPar[nPos] = nMinPos;
    }
    else
    {
     pPar[nPos] = nPos;
     pVal[nPos] = 'a'+nChar;
     nChar++;
    }
   }
  }
  nPos = 0;
  for(y=0; y<nHeight; y++)
  {
   for(x=0; x<nWidth; x++, nPos++)
   {
    SetVal(pVal, pPar, nPos);
   }
  }
  
  char* pMask = (char*)malloc(nChar*sizeof(char));
  int* pMinPos = (int*)malloc(nChar*sizeof(int));
  for(x=0; x<nChar; x++)
  {
   pMask[x] = x;
   pMinPos[x] = 0xFFFFFF;
  }
  nPos = 0;
  for(y=0; y<nHeight; y++)
  {
   for(x=0; x<nWidth; x++, nPos++)
   {
    if(nPos<pMinPos[pVal[nPos]-'a'])
    {
     pMinPos[pVal[nPos]-'a'] = nPos;
    }
   }
  }

  for(y=0; y<nChar; y++)
  {
   for(x=0; x<nChar; x++)
   {
    if(pMinPos[y]<pMinPos[x])
    {
     int nTmp;
     nTmp = pMinPos[y];
     pMinPos[y] = pMinPos[x];
     pMinPos[x] = nTmp;
     nTmp = pMask[y];
     pMask[y] = pMask[x];
     pMask[x] = nTmp;
    }
   }
  }
  printf("Case #%d:/n", i+1);
  nPos = 0;
  for(y=0; y<nHeight; y++)
  {
   for(x=0; x<nWidth; x++, nPos++)
   {
    printf("%c ", pMask[pVal[nPos]-'a']+'a');
   }
   printf("/n");
  }

  free(pMask);
  free(pHei);
  free(pPar);
  free(pVal);
 }
 

 

 fclose(fp);
 return 0;
}

%DUMP LIBRARIES-List Loaded PSL Libraries 阅读详情

相关推荐

SNN目标识别MNIST

总体来说这篇论文提出的方法用了多种更精密的细节,如LIF膜电位公式,三种STDP方法,但我觉得还可以有很大的改进,比如输入层全连接到兴奋层,兴奋层通过抑制层实现的侧向抑制,抑制层不参与学习等等。尤其是兴奋层通过抑制层实现侧向抑制这里,当神经元兴奋时,通过抑制层再倒回兴奋层,所抑制的已经经过一了一段时间的延迟。如有错误,请不吝赐教。

m0_74982304的博客 1243

google code jam 2009资格赛(pass)

第一,字符串查找。用hash存字典,然后把可组合成的串与字典匹配。因为串的长度是固定的,可以把字典分解着存,这样无法匹配的串就可以提前退出。比如说abcde是字典中的串,则a,ab,abc,abcd,abcde都存到字典里去。我做的时候,大数据没跑完,当时我还以为是算法问,后来发现,是因为我把字符串分解着存,hash数组开得太小了。囧。后来一朋友告诉我一种很好的做法,不要用未知串来匹配已经串,

low coder 613

倾斜摄影数据转换为3DTile格式的方法:基于CesiumLab GIS

通过使用CesiumLab GIS,倾斜摄影数据可以方便地转换为3DTile格式,并在CesiumJS等3D地理信息可视化工具中进行展示和分析。一旦成功导入3DTile数据,就可以在CesiumLab GIS中利用其强大的可视化和分析功能进行进一步的处理。转换完成后,可以将生成的3DTile数据导入到CesiumLab GIS中进行可视化和分析。启动CesiumLab GIS应用程序,并按照界面上的指导导入转换后的3DTile数据。脚本将开始执行,并将倾斜摄影数据转换为3DTile格式。

ObDjango的博客 1053

我的google code jam 2009结束了

  google code jam 2009全球编程挑战赛round2刚刚结束,我rank624,没有进入top500,那件谷歌的T恤终究没有拿到手。好吧,先寄存在那,我明年来拿。  想想从暑假开始正式做acm,到现在已经有差不多2个半月了,pku上目前显示我做了152道目,加上其他oj以及一些google,我的总量差不多在200的样子。  暑假的时候为什么要做acm呢?记得最

low coder 1349

Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)

Problem In a kingdom there are prison cells (numbered 1 toP) built to form a straight line segment. Cells numberiandi+1are adjacent, and prisoners in adjacent cells are called "neighbour...

weixin_30527323的博客 217

Google Code Jam 2009资格赛 目B Watersheds

分水岭Problem 问Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins. 地址学家有时候根据降雨量把一个陆地地区分成不同的区域。这些区域被称

xbl1986的专栏 909

Google Code Jam预选赛完成.

一共三道目, 目本身不是很难.第一只要遍历就可以.第二并查集做就可以了,我输出的时候字母没有按顺序,结果递交了4次uncorrect, 查了老半天,郁闷.第三动态规划算法轻松解决. 从效率考虑,即使是Large Set, 也是秒杀, 所以一般small set通过, big set直接就过了. 第一名那位只花了25:41时间, 不愧是牛人啊, 我用上金山词霸,

林建华的专栏 1339

Google Code Jam

    Google从2003年起举办Google Code Jam程序设计竞赛(http://code.google.com/codejam/contest/),用意在提升全球程序开发者交流,同时嘉奖优秀的程序设计者。    粗略的看了一下Code Jam, 与ACM有类似之处, 但是测试的数据是开放的, 并且分为small和big两部分, small主要是用来验证程序的正确性,而big则是

林建华的专栏 3739

Egg Drop

http://code.google.com/codejam/contest/dashboard?c=agxjb2RlamFtLXByb2RyEAsSCGNvbnRlc3RzGIP6AQw#s=p2 ProblemImagine that you are in a building with F floors (starting at floor 1, the lowest floor

林建华的专栏 2526

Google Code Jam 2009预选赛第一----Alien Language

ProblemAfter years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L

林建华的专栏 1583

Alien Numbers

http://code.google.com/codejam/contest/dashboard?c=agxjb2RlamFtLXByb2RyEAsSCGNvbnRlc3RzGIP6AQw#ProblemThe decimal numeral system is composed of ten digits, which we represent as "0123456789" (the

林建华的专栏 1222

map,list简单方法

System.out.println(word1.stream().filter(a -> a.length() > 4).findFirst().orElse("没有找到"));// System.out.println(num3.stream().allMatch(new Predicate<Integer>() { // 等价匿名内部类版。

2301_80007421的博客 209

C++第十讲:list

list 是 C++ 标准库提供的双向循环链表容器,每个元素存储在独立的节点中,节点通过指针连接。底层结构:带头结点的双向循环链表特点:✅ 任意位置插入删除效率 O (1)(不需要搬移元素)❌ 不支持随机访问(不能用[],访问元素需要遍历,O (N))没有容量概念,节点动态申请释放​// 指向节点的指针​{}​// 解引用:返回节点数据的引用​// 箭头:返回节点数据的指针​// 前置++​// 后置++​// 前置--​// 后置--

weixin_54085376的博客 432

Java List 接口详解:从基础到实战

List 接口继承自 Collection 接口,位于 java.util 包中。它允许存储重复元素,并且每个元素都有对应的索引位置,可以通过索引精确访问元素。与 Set 不同,List 保持了元素的插入顺序。有序性:元素按照插入顺序排列,可通过索引访问。可重复:允许存储相同的元素。允许 null:大多数实现类允许存储 null 值。索引访问:支持通过下标快速定位元素。List 是 Java 集合框架中最重要的接口之一,掌握它的实现原理、常用方法和遍历方式,是写出高质量 Java 代码的基础。

2601_96129122的博客 237

C++ STL 容器深度剖析:vector 与 list 的模拟实现及对比

vector与list模拟实现

2402_87037360的博客 229

⚡Spring JDBC 完整体系 · 第 9 集 | 封装 list 方法 · 泛型初探

<think>我们只需要从给定内容生成一篇≤150字的摘要。内容是关于Spring JDBC封装list方法、泛型初探的。需要概括核心:项目背景、痛点(每次new RowMapper)、解决方案(BaseDao封装list泛型方法,传入SQL和Class,内部用BeanPropertyRowMapper)。注意字数限制,抓重点。直接输出摘要,不要其他格式。</think>本集针对Spring JDBC中查询需反复手动创建BeanPropertyRowMapper的痛点,设计了一个通用BaseDao并封装泛型

gzz_gzz的博客 442

list(C++)

<think>我们根据要求生成≤150字的摘要。内容是关于std::list的特点、使用和底层实现。注意字数限制,需要精炼。</think>摘要:本文讲解C++ STL中std::list双向链表容器的原理与使用。其优势是任意位置O(1)插入删除,迭代器不易失效;缺点是不支持随机访问、内存开销大。介绍了增删改查、迭代器特性及splice、sort等专属操作,并强调不能使用std::sort。最后简要模拟实现迭代器与核心接口,指导开发者按场景选择合适的容器。

2401_87882972的博客 274

06-04-排序集合-SortedList-TKey-TValue-双数组实现的有序集合

二分查找是 O(log n),后缀搬移是 O(n),所以中间插入总复杂度是 O(n)。随机生成 Add、setter、Remove、Clear 和查询序列,每步检查 Count、键顺序、键值对应和重复键行为。大 O 不告诉转折点。搬移后,原有最后一个有效槽会留下重复引用,实现应在 TKey/TValue 是引用或含引用时将尾槽置为 default,避免已删对象被后备数组继续保活。二分查找使按键查询为 O(log n),连续布局使枚举和已知索引访问紧凑,中间插入/删除则因双数组搬移为 O(n)。

设计 AI 玩游戏🎮 1005

C++ list 深度解析:从使用原理到模拟实现

list 是带头结点的双向循环链表,插入删除 O(1),无法随机访问。其迭代器通过 Ref/Ptr 模板区分普通与 const 版本;注意 typename 语法陷阱。因节点零散,排序比 vector 慢,可拷入 vector 排序再拷回优化。splice/merge 仅改指针,效率极高。适合频繁插入删除、无需随机访问的场景。

xxwxx__的博客 342

linux挂载img镜像文件

本文主要介绍linux挂载img镜像文件的方法。

上一篇: Google Code Jam 2009预选赛第一题----Alien Language
下一篇: Google Code Jam 2009预选赛第三题----Welcome to Code Jam
fire_woods
博客等级 码龄24年 408粉丝 108原创
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值