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. 微信小程序开闸,关于入口、推广、场景的一些观察与思考

    今夜(1月9号)零点,微信小程序正式上线.在体验了很多款小程序后,我对小程序的使用场景有了更多的认识.以下是一些想法,欢迎交流. 一.小程序的"入口"在哪儿? 1.只有访问过的小程 ...

  2. Servlet单实例多线程模式

    http://kakajw.iteye.com/blog/920839 前言:Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率.由于Servlet/JSP默认是以 ...

  3. JDBC对sql server的操作

    1.过程: 1>注册驱动器类:Class.forName()       2>连接数据库:             String url = "jdbc:sqlserver:// ...

  4. Java Web应用启动间隔执行的程序

    Reference:<Java定时器timer.schedule在Web中间隔执行任务和定时><[Java]Timer和TimerTask详解> 做了一个Demo,完成如下的功 ...

  5. 使用sqlldr命令导入资料到Oracle数据库表中的示例说明

    CSV: Comma-Separated Values(逗号分隔值)的缩写,是以逗号分隔字段的多行文本文件   sqlldr 是sql*loader的缩写,此工具在安装完整版的Oracle客户端后就有 ...

  6. JavaScript高级程序设计(四): 关键字With的使用

    一.关键字with 1.含义    with 语句可以方便地用来引用某个特定对象中已有的属性,但是不能用来给对象添加属性.要给对象创建新的属性,必须明确地引用该对象.   简单的说,with为一个或一 ...

  7. MarkDown 格式生产类型

    -- 不默认换行, 真的结束, 包括格式设定,记得空一行. -- 学习参考地址如下, 讲的不好, 太复杂, 不适合新手. 有好读的更好. ** 但是江湖规矩 还是引用下吧 这是地址(http://wo ...

  8. Malformed network data

    Malformed network data

  9. EF 存储过程(上)

    目前,EF对存储过程的支持并不完善.存在以下问题:> EF不支持存储过程返回多表联合查询的结果集.> EF仅支持返回某个表的全部字段,以便转换成对应的实体.无法支持返回部分字段的情况.&g ...

  10. starling 中的 EventDispatcher 和 Flash中原生的 EventDispatcher

    starling 比较早之前就有开始解了,但只到最近参与一个用starling 做为框架的手游项目才真正做为一程来使用它. 项目也是刚开始搭建,在这做些笔记. 在写一个管理类时, 遇到 starlin ...