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. 理解Objective-c中的copy

    说一下深拷贝和浅拷贝的基本概念:a指针指向地址A1, 浅拷贝是创建了一个b指针指向地址A1:深拷贝是创建了一个c指针指向地址A2,A1和A2的地址不同. 我们看到NSObject接口里面是已经声明了c ...

  2. (转)HttpHandler与HttpModule的理解与应用

    神秘的HttpHandler与HttpModule 大学时候我是从拖控件开始学习 asp.net的,对.net的很多类库对象都不是很了解.所以看到大家写一些个性的asp.net名词,就感觉asp.ne ...

  3. Xcode8 Could not build Objective-C module 'FBSDKCoreKit'

    解决方法是: 删除/Users/Rinpe/Library/Developer/Xcode/DerivedData下对应的文件夹即可.

  4. autorelease 的基本使用

    5-autorelease 的基本使用 0,引入 Person *p = [Persom new];[p release]; [p run]; [p run]; // 希望不立即释放,待 run执行完 ...

  5. HDU 2502 月之数(简单递推)

    月之数 Problem Description 当寒月还在读大一的时候,他在一本武林秘籍中(据后来考证,估计是计算机基础,狂汗-ing),发现了神奇的二进制数.如果一个正整数m表示成二进制,它的位数为 ...

  6. 什么是CGI(Common Gateway Interface)?

    参考: 1.Python CGI编程 2.十分钟搞懂CGI 3.CGI Made Really Easy

  7. Parameters

    Quote from: http://ss64.com/nt/syntax-args.html Parameters A parameter (or argument) is any value pa ...

  8. GitCam一款Gif动画制作软件

    本篇文章由:http://www.sollyu.com/gitcam-a-gif-animation-software/ 说明 GifCam是一款小巧.免费的录制电脑屏幕并制作成GIF动画的软件,具有 ...

  9. boost库----enable_shared_from_this类的作用和实现原理

    使用boost库时,经常会看到如下的类 class A:public enable_share_from_this<A> 在什么情况下要使类A继承enable_share_from_thi ...

  10. IOS视图旋转可放大缩小

    - (IBAction)hideBut:(id)sender { if (self.flg) { [UIView animateWithDuration:0.3 animations:^{ self. ...