[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 ...
随机推荐
- ie各个版本hack
/*类内部hack:*/ .header {_width:100px;} /* IE6专用*/ .header {*+width:100px;} /* IE7专用*/ .header {*width: ...
- 20160327javaweb 之JSP入门
一.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于,写jsp就像在写h ...
- Solr4.8.1与Tomcat7整合
Solr4.8.1和Tomcat7都可以到官方网站去下载,我这里就不多说了,如下图. 这里我们首先解压Solr-4.8.1.zip,再解压Tomcat,解压后,再在当前文件夹下建2个文件夹,一个用来放 ...
- jQuery $.each的使用方法
通过jQuery $.each,你可以遍历对象.数组的属性值并进行处理. 使用说明 each函数根据参数的类型实现的效果不完全一致: 1.遍历对象(有附加参数) $.each(Object, func ...
- ios毛玻璃效果
方法一:支持所有ios系统版本: - (void)setupBlurView { UIImageView *darkView = [[UIImageView alloc] init]; darkVie ...
- UIView的frame和bounds区别
UIView的frame和bounds区别 iOS中,大家肯定对view和frame都不陌生,我们设置view在父view中的位置和大小时,只需要设置frame就可以了. 可能大家也有查过网上的一些资 ...
- c语言指针字符串与字符数组字符串的区别
#include <stdio.h> int main() { //字符串常量,存放于内存常量区. //常量区区的内存具有缓存机制, //当不同指针指向的常量值相同时, //其实这些指针指 ...
- MFC Tips(一) 在程序内部 保存读取配置
//保存 CWinApp *pApp = AfxGetApp(); pApp->WriteProfileBinary(..); //保存结构体 pApp->WriteProfileInt( ...
- C#中2、8、16进制 有符号转换10进制正负数
曾经让我苦想的其他进制转有符号整型问题,结果自己想到方法解决后才发现原来如此简单. 1.Int16(2个byte长度 ) : 方法 :Convert.ToInt16(进制编码,进制) a.16进制转1 ...
- 手把手教你写电商爬虫-第四课 淘宝网商品爬虫自动JS渲染
版权声明:本文为博主原创文章,未经博主允许不得转载. 系列教程: 手把手教你写电商爬虫-第一课 找个软柿子捏捏 手把手教你写电商爬虫-第二课 实战尚妆网分页商品采集爬虫 手把手教你写电商爬虫-第三课 ...