BFS。这里用了queue,以及在数据结构里存了上一个的高度。也可以递归调用完成BFS,在调用之前做判断:http://community.topcoder.com/stat?c=problem_solution&cr=9958883&rd=5856&pm=2932

import java.util.*;
public class TopographicalImage
{
public int[] calcPeakAreas(String[] topoData)
{
int m = topoData.length;
int n = topoData[0].length();
ArrayList<Integer> ans = new ArrayList<Integer>();
int total = 0;
boolean[][] visited = new boolean[m][n];
while (total < m * n)
{
int max = -1;
Pair p = new Pair(0, 0, 0);
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
if (!visited[i][j] && topoData[i].charAt(j) > max)
{
max = topoData[i].charAt(j);
p.x = i;
p.y = j;
p.lastHeight = max;
}
}
}
Queue<Pair> queue = new LinkedList<Pair>();
queue.add(p);
int cnt = 0;
while (queue.size() != 0)
{
Pair t = queue.poll();
int h = topoData[t.x].charAt(t.y);
if (!visited[t.x][t.y] && h <= t.lastHeight)
{
visited[t.x][t.y] = true;
cnt++;
if (t.y + 1 < n) queue.add(new Pair(t.x, t.y + 1, h));
if (t.x + 1 < m) queue.add(new Pair(t.x + 1, t.y, h));
if (t.y - 1 >= 0) queue.add(new Pair(t.x, t.y - 1, h));
if (t.x - 1 >= 0) queue.add(new Pair(t.x - 1, t.y, h));
if (t.x + 1 < m && t.y + 1 < n) queue.add(new Pair(t.x + 1, t.y + 1, h));
if (t.x + 1 < m && t.y - 1 >= 0) queue.add(new Pair(t.x + 1, t.y - 1, h));
if (t.x - 1 >= 0 && t.y - 1 >= 0) queue.add(new Pair(t.x - 1, t.y - 1, h));
if (t.x - 1 >= 0 && t.y + 1 < n) queue.add(new Pair(t.x - 1, t.y + 1, h));
}
}
ans.add(cnt);
total += cnt;
}
int[] res = new int[ans.size()];
for (int i = 0; i < ans.size(); i++)
{
res[i] = ans.get(i);
}
return res;
}
} class Pair
{
int x;
int y;
int lastHeight;
public Pair(int _x, int _y, int _lastHeight)
{
x = _x;
y = _y;
lastHeight = _lastHeight;
}
}

  

[topcoder]TopographicalImage的更多相关文章

  1. TopCoder kawigiEdit插件配置

    kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...

  2. 记第一次TopCoder, 练习SRM 583 div2 250

    今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...

  3. TopCoder比赛总结表

    TopCoder                        250                              500                                 ...

  4. Topcoder几例C++字符串应用

    本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...

  5. TopCoder

    在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...

  6. TopCoder SRM 596 DIV 1 250

    body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...

  7. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  8. TopCoder SRM 590

     第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement     Fox Ciel is going to play Gomoku with her friend ...

  9. Topcoder Arena插件配置和训练指南

    一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...

随机推荐

  1. PHP + ajax 实现异步登录验证

    login.html: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http:/ ...

  2. Fragment碎片频繁来回切换的时候报java.lang.IllegalStateException: No activity

    出现这个问题的原因是因为使用的transcation.replace(fragmentTwo);的方式进行碎片切换的. 解决方案是使用add和show.hide方法组合实现碎片的切换(应该是显示.隐藏 ...

  3. ThinkPHP函数详解:cache方法

    cache方法是3.0版本开始新增的缓存管理方法.注意:3.1.2版本后因cache方法并入原S方法,所以cache方法不再建议使用,用S方法即可. cache 用于缓存设置.获取.删除操作 用法ca ...

  4. SQL Server delete、truncate、drop

    在T-SQL中这三个命令符,相信很多朋友都不会陌生的,我自己在工作也会常常使用到它们,虽然我们清除的知道用这三个命令符可以达到怎样的预期效果. 但是却很少深入的去了解它们,知道它们有什么区别,又各有什 ...

  5. SQLServer2012分离出的数据库存放路径

    分离出的数据库没有保存位置提示,经常会导致分离出的数据库找不到  以下是分离出的数据库默认位置: C:\Program Files\Microsoft SQL Server\MSSQL11.MSSQL ...

  6. 用正则表达式解析XML

    import java.util.regex.*; import java.util.*; /** * * <p>Title: Document</p> * * <p&g ...

  7. (五)Hibernate 操作对象

    所有项目导入对应的hibernate的jar包.mysql的jar包和添加每次都需要用到的HibernateUtil.java 第一节:Hibernate 中四种对象状态 临时状态(transient ...

  8. 关于arcgis 9.3破解问题详解

    对于初学GIS的同学,安装软件可能会遇到各种各样的问题,对于photoshop,autocad,sketchup,3dmax等软件我们的我们无非是输入特定序列号或者用工具随机生成特定序列号就可以破解, ...

  9. 《程序员的思维修炼》摘抄start:2014年9月27日19:27:07

    程序员的思维修炼:摘抄:考虑到社会中各个相关团体的复杂交互影响和社会的持续变化,在我看来当前最重要的两项技能就是: ▪沟通能力: ▪学习和思考能力.软件行业正在逐步提高沟通能力.特别是敏捷方法(见注解 ...

  10. Codevs 1689 建造高塔

    1689 建造高塔 时间限制: 1 s 空间限制: 128000 KB 题目等级 : **钻石 Diamond** 题目描述 Description n有n种石块,石块能无限供应.每种石块都是长方体, ...