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

  1. import java.util.*;
  2. public class TopographicalImage
  3. {
  4. public int[] calcPeakAreas(String[] topoData)
  5. {
  6. int m = topoData.length;
  7. int n = topoData[0].length();
  8. ArrayList<Integer> ans = new ArrayList<Integer>();
  9. int total = 0;
  10. boolean[][] visited = new boolean[m][n];
  11. while (total < m * n)
  12. {
  13. int max = -1;
  14. Pair p = new Pair(0, 0, 0);
  15. for (int i = 0; i < m; i++)
  16. {
  17. for (int j = 0; j < n; j++)
  18. {
  19. if (!visited[i][j] && topoData[i].charAt(j) > max)
  20. {
  21. max = topoData[i].charAt(j);
  22. p.x = i;
  23. p.y = j;
  24. p.lastHeight = max;
  25. }
  26. }
  27. }
  28. Queue<Pair> queue = new LinkedList<Pair>();
  29. queue.add(p);
  30. int cnt = 0;
  31. while (queue.size() != 0)
  32. {
  33. Pair t = queue.poll();
  34. int h = topoData[t.x].charAt(t.y);
  35. if (!visited[t.x][t.y] && h <= t.lastHeight)
  36. {
  37. visited[t.x][t.y] = true;
  38. cnt++;
  39. if (t.y + 1 < n) queue.add(new Pair(t.x, t.y + 1, h));
  40. if (t.x + 1 < m) queue.add(new Pair(t.x + 1, t.y, h));
  41. if (t.y - 1 >= 0) queue.add(new Pair(t.x, t.y - 1, h));
  42. if (t.x - 1 >= 0) queue.add(new Pair(t.x - 1, t.y, h));
  43. if (t.x + 1 < m && t.y + 1 < n) queue.add(new Pair(t.x + 1, t.y + 1, h));
  44. if (t.x + 1 < m && t.y - 1 >= 0) queue.add(new Pair(t.x + 1, t.y - 1, h));
  45. if (t.x - 1 >= 0 && t.y - 1 >= 0) queue.add(new Pair(t.x - 1, t.y - 1, h));
  46. if (t.x - 1 >= 0 && t.y + 1 < n) queue.add(new Pair(t.x - 1, t.y + 1, h));
  47. }
  48. }
  49. ans.add(cnt);
  50. total += cnt;
  51. }
  52. int[] res = new int[ans.size()];
  53. for (int i = 0; i < ans.size(); i++)
  54. {
  55. res[i] = ans.get(i);
  56. }
  57. return res;
  58. }
  59. }
  60.  
  61. class Pair
  62. {
  63. int x;
  64. int y;
  65. int lastHeight;
  66. public Pair(int _x, int _y, int _lastHeight)
  67. {
  68. x = _x;
  69. y = _y;
  70. lastHeight = _lastHeight;
  71. }
  72. }

  

[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. 一个项目覆盖CS所有课程的可行性探究

    我们先看计算机科学有哪些子领域. 学术领域有: 计算理论 信息和编码理论 算法和数据结构 形式化方法 程序设计语言 实践领域有: 计算机体系结构 并行计算和分布式系统 实时系统和嵌入式系统 操作系统 ...

  2. jquery基础-包裹 替换 删除 复制

    <!doctype html><html lang="en"><head>    <meta charset="UTF-8&qu ...

  3. 例子: 自制Flask首页导航.

    # -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask, render_template impor ...

  4. 01_JavaMail_04_带附件邮件的发送

    [工程截图] [代码实例] package com.Higgin.mail.demo; import java.io.File; import java.util.Properties; import ...

  5. 文件(夹)比较 Beyond Compare, Diff

    文件(夹)比较 Beyond Compare, Diff, UltraCompare 1.Beyond Compare(无与伦比) 2.Diff 参考 1.diff详解

  6. mysql导入导出.sql文件 备份还原数据库

    从数据库导出数据库文件:   进入你的MySQL的安装目录的bin目录或者在C盘的根目录都行,我选的是在bin目录下,下面的例子出第一个外将以在C盘的根目录来讲解   我的mysql安装在了C盘,C: ...

  7. Quartz 之 windowService

    (一)创建服务 QuarzService using System.ServiceProcess;using System.Text; using Quartz;using Quartz.Impl; ...

  8. React组件一

    <div id='test'></div> <script type='text/babel'> var Zu=React.createClass({ return ...

  9. YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE

    yii2安装后,连接数据库,必须要安装pdo_mysql扩展

  10. node 无解回调 有解了

    http://cssor.com/javascript-workflow-by-tofishes.html