[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 ...
随机推荐
- 一个项目覆盖CS所有课程的可行性探究
我们先看计算机科学有哪些子领域. 学术领域有: 计算理论 信息和编码理论 算法和数据结构 形式化方法 程序设计语言 实践领域有: 计算机体系结构 并行计算和分布式系统 实时系统和嵌入式系统 操作系统 ...
- jquery基础-包裹 替换 删除 复制
<!doctype html><html lang="en"><head> <meta charset="UTF-8&qu ...
- 例子: 自制Flask首页导航.
# -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask, render_template impor ...
- 01_JavaMail_04_带附件邮件的发送
[工程截图] [代码实例] package com.Higgin.mail.demo; import java.io.File; import java.util.Properties; import ...
- 文件(夹)比较 Beyond Compare, Diff
文件(夹)比较 Beyond Compare, Diff, UltraCompare 1.Beyond Compare(无与伦比) 2.Diff 参考 1.diff详解
- mysql导入导出.sql文件 备份还原数据库
从数据库导出数据库文件: 进入你的MySQL的安装目录的bin目录或者在C盘的根目录都行,我选的是在bin目录下,下面的例子出第一个外将以在C盘的根目录来讲解 我的mysql安装在了C盘,C: ...
- Quartz 之 windowService
(一)创建服务 QuarzService using System.ServiceProcess;using System.Text; using Quartz;using Quartz.Impl; ...
- React组件一
<div id='test'></div> <script type='text/babel'> var Zu=React.createClass({ return ...
- YII2数据库操作出现类似Database Exception – yii\db\Exception SQLSTATE
yii2安装后,连接数据库,必须要安装pdo_mysql扩展
- node 无解回调 有解了
http://cssor.com/javascript-workflow-by-tofishes.html