[topcoder]TopographicalImage
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的更多相关文章
- TopCoder kawigiEdit插件配置
kawigiEdit插件可以提高 TopCoder编译,提交效率,可以管理保存每次SRM的代码. kawigiEdit下载地址:http://code.google.com/p/kawigiedit/ ...
- 记第一次TopCoder, 练习SRM 583 div2 250
今天第一次做topcoder,没有比赛,所以找的最新一期的SRM练习,做了第一道题. 题目大意是说 给一个数字字符串,任意交换两位,使数字变为最小,不能有前导0. 看到题目以后,先想到的找规律,发现要 ...
- TopCoder比赛总结表
TopCoder 250 500 ...
- Topcoder几例C++字符串应用
本文写于9月初,是利用Topcoder准备应聘时的机试环节临时补习的C++的一部分内容.签约之后,没有再进行练习,此文暂告一段落. 换句话说,就是本文太监了,一直做草稿看着别扭,删掉又觉得可惜,索性发 ...
- TopCoder
在TopCoder下载好luncher,网址:https://www.topcoder.com/community/competitive%20programming/ 选择launch web ar ...
- TopCoder SRM 596 DIV 1 250
body { font-family: Monospaced; font-size: 12pt } pre { font-family: Monospaced; font-size: 12pt } P ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- TopCoder SRM 590
第一次做TC,不太习惯,各种调试,只做了一题...... Problem Statement Fox Ciel is going to play Gomoku with her friend ...
- Topcoder Arena插件配置和训练指南
一. Arena插件配置 1. 下载Arena 指针:http://community.topcoder.com/tc?module=MyHome 左边Competitions->Algorit ...
随机推荐
- commons-fileupload源码学习心得
commons-fileupload依赖于commons-io包. commons-fileupload的使用方法: 1.创建一个文件项目工厂类DiskFileItemFactory. D ...
- struts1与struts2的区别
Struts2其实并不是一个陌生的Web框架,Struts2是以Webwork的设计思想为核心,吸收了Struts1的优点,因此,可以认为Struts2是Struts1和Webwork结合的产物. 简 ...
- 浅谈C#随机数发生器
我们在做能自动生成试卷的考试系统时,常常需要随机生成一组不重复的题目,在.net Framework中提供了一个专门用来产生随机数的类System.Random. 对于随机数,大家都知道,计算机不 可 ...
- string应用
今天在网上搜了一些资料. C# string类应用 判断是否包含子串 想要判断一个字符串中是否包含某个子串,可以用Contains方法来实现: ? public bool Contains (stri ...
- JDBC对sql server的操作
1.过程: 1>注册驱动器类:Class.forName() 2>连接数据库: String url = "jdbc:sqlserver:// ...
- jsf taglib定义函数
创建文件 在文件中添加function标签 <function> <function-name>getFileContent</function-name> & ...
- Java文件操作 读写操作
一.Java读取文件 案例1:读取D盘的1.txt文件 编码: File file = new File("D:/1.txt"); FileReader fr = new File ...
- java数据同步陷阱
并发,我的理解就是同时运行多个程序.同时,难以避免的就是数据的同步问题,如果数据同步问题处理不好就很容易造成程序出现bug,当然,对于其造成的危害,不加详述. 首先,来看一个简单的例子,当然,这个例子 ...
- Codevs 1138 聪明的质监员 2011年NOIP全国联赛提高组
1138 聪明的质监员 2011年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 小 T 是一名质量监督员, ...
- (转)linux多线程,线程的分离与结合
转自:http://www.cnblogs.com/mydomain/archive/2011/08/14/2138454.htm 线程的分离与结合 在任何一个时间点上,线程是可结合的(joi ...