本地测试 ,没有错误,提交后 runtime error
Description IONU Satellite Imaging, Inc. records and stores very large images using run length encoding. You are to write a program that reads a compressed image, finds the edges in the image, as described below, and outputs another compressed image of the detected edges. A simple edge detection algorithm sets an output pixel's value to be the maximum absolute value of the differences between it and all its surrounding pixels in the input image. Consider the input image below: The upper left pixel in the output image is the maximum of the values |15-15|,|15-100|, and |15-100|, which is 85. The pixel in the 4th row, 2nd column is computed as the maximum of |175-100|, |175-100|, |175-100|, |175-175|, |175-25|, |175-175|,|175-175|, and |175-25|, which is 150. Images contain 2 to 1,000,000,000 (109) pixels. All images are encoded using run length encoding (RLE). This is a sequence of pairs, containing pixel value (0-255) and run length (1-109). Input images have at most 1,000 of these pairs. Successive pairs have different pixel values. All lines in an image contain the same number of pixels. Input Input consists of information for one or more images. Each image starts with the width, in pixels, of each image line. This is followed by the RLE pairs, one pair per line. A line with 0 0 indicates the end of the data for that image. An image width of 0 indicates there are no more images to process. The first image in the example input encodes the 5x7 input image above. Output Output is a series of edge-detected images, in the same format as the input images, except that there may be more than 1,000 RLE pairs. Sample Input 7 15 4 100 15 25 2 175 2 25 5 175 2 25 5 0 0 10 35 500000000 200 500000000 0 0 3 255 1 10 1 255 2 10 1 255 2 10 1 255 1 0 0 0 Sample Output 7 85 5 0 2 85 5 75 10 150 2 75 3 0 2 150 2 0 4 0 0 10 0 499999990 165 20 0 499999990 0 0 3 245 9 0 0 0 Hint A brute force solution that attempts to compute an output value for every individual pixel will likely fail due to space or time constraints.
import java.math.BigDecimal; import java.nio.Buffer; import java.util.ArrayList; import java.util.List; import java.util.Scanner; import java.util.Set; import java.util.TreeSet; import org.omg.CORBA.DATA_CONVERSION; /** * * @author baoyou E-mail:curiousby@163.com * @version 创建时间:2015年10月3日 下午11:43:37 * des: */ public class BaoyPoj1009Test2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); for (;;) { int cols = in.nextInt(); BigDecimal num = new BigDecimal(0); System.out.println(cols); List<Node> target = new ArrayList<Node>(); for (;;) { int a = in.nextInt(); BigDecimal b = in.nextBigDecimal(); num = num.add(b); if (a != 0 && b.intValue() != 0) target.add(new Node(a, target.size() == 0 ? b : b.add(target .get(target.size() - 1).num))); if (a == 0 && b.intValue() == 0) { BigDecimal rows = num.divide(new BigDecimal(cols), BigDecimal.ROUND_DOWN); Set<DataNode> source = new TreeSet<DataNode>(); BigDecimal current = new BigDecimal(1); find9(cols, rows, current, target , source); for (int i = 0; i < target.size()-1; i++) { Node node = target.get(i); current = new BigDecimal(1).add(node.num); find9(cols, rows, current, target , source); } boolean first =true; int absResult=0; BigDecimal numTemp= new BigDecimal(0); BigDecimal numResult= new BigDecimal(0); for ( DataNode dnode:source) { if (first) { absResult = dnode.abs; numResult = dnode.start; first = false; }else{ if (absResult == dnode.abs) { //numResult = dnode.start; }else{ System.out.println(absResult + " " + dnode.start.subtract(numResult)); absResult = dnode.abs; numResult = dnode.start; } } } System.out.println(absResult + " " + num.subtract(numResult).add(new BigDecimal(1))); System.out.printf("%d %d\r\n", 0, 0); break; } } if (cols == 0) { System.out.println(0); break; } } } public static void find9(int cols, BigDecimal rows, BigDecimal i, List<Node> target ,Set<DataNode> source){ BigDecimal start = new BigDecimal(0); int abs = 0; //self start = i; if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } // 上 (x,y-1) if (i.compareTo(new BigDecimal(cols)) > 0) { start = i.subtract(new BigDecimal(cols)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 下 (x,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) <= 0) { start = i.add(new BigDecimal(cols)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 左 (x-1,y) if (i.remainder(new BigDecimal(cols)).compareTo(new BigDecimal(1)) != 0) { start = i.subtract(new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows,start, target); source.add(new DataNode(start,abs)); } } // 右(x+1,y) if (i.remainder(new BigDecimal(cols)).compareTo(new BigDecimal(0)) != 0) { start = i.add(new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 左上 (x-1,y-1) if (i.compareTo(new BigDecimal(cols)) > 0 && i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(1)) != 0) { start = i.subtract(new BigDecimal(cols)).subtract( new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 右上 (x+1,y-1) if (i.compareTo(new BigDecimal(cols)) > 0 && i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(0)) != 0) { start = i.subtract(new BigDecimal(cols)).add(new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 左下 (x-1,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) <= 0 && i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(1)) != 0) { start = i.add(new BigDecimal(cols)).subtract(new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } // 右下 (x+1,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) <= 0 && i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(0)) != 0) { start = i.add(new BigDecimal(cols)).add(new BigDecimal(1)); if (!source.contains(new DataNode(start))) { abs = maxABS(cols, rows, start, target); source.add(new DataNode(start,abs)); } } } public static int maxABS(int cols, BigDecimal rows, BigDecimal i, List<Node> target) { int max = 0; int current = getValue(i, target); BigDecimal location = new BigDecimal(0); // 上 (x,y-1) if (i.compareTo(new BigDecimal(cols)) <= 0) { int a = 0; if (a > max) max = a; } else { location = i.subtract(new BigDecimal(cols)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 下 (x,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) > 0) { int a = 0; if (a > max) max = a; } else { location = i.add(new BigDecimal(cols)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 左 (x-1,y) if (i.remainder(new BigDecimal(cols)).compareTo(new BigDecimal(1)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.subtract(new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 右(x+1,y) if (i.remainder(new BigDecimal(cols)).compareTo(new BigDecimal(0)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.add(new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 左上 (x-1,y-1) if (i.compareTo(new BigDecimal(cols)) <= 0) { int a = 0; if (a > max) max = a; } else if (i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(1)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.subtract(new BigDecimal(cols)).subtract( new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 右上 (x+1,y-1) if (i.compareTo(new BigDecimal(cols)) <= 0) { int a = 0; if (a > max) max = a; } else if (i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(0)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.subtract(new BigDecimal(cols)).add(new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 左下 (x-1,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) > 0) { int a = 0; if (a > max) max = a; } else if (i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(1)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.add(new BigDecimal(cols)).subtract(new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } // 右下 (x+1,y+1) if (i.compareTo(rows.subtract(new BigDecimal(1)).multiply( new BigDecimal(cols))) > 0) { int a = 0; if (a > max) max = a; } else if (i.remainder(new BigDecimal(cols)).compareTo( new BigDecimal(0)) == 0) { int a = 0; if (a > max) max = a; } else { location = i.add(new BigDecimal(cols)).add(new BigDecimal(1)); int a = Math.abs(getValue(location, target) - current); if (a > max) max = a; } return max; } public static int getValue(BigDecimal location, List<Node> target) { for (Node d : target) { if (d.num.compareTo(location) >= 0) return d.ascii; } return 0; } static class Node { public int ascii; public BigDecimal num; public Node(int ascii, BigDecimal num) { this.ascii = ascii; this.num = num; } @Override public String toString() { StringBuffer sb= new StringBuffer(); sb.append("{") .append("ascii:"+ascii) .append(",num:"+num) .append("}"); return sb.toString(); } } static class DataNode implements Comparable{ public BigDecimal start; public int abs; public DataNode(BigDecimal start){ this.start = start; } public DataNode(BigDecimal start, int abs) { this.start = start; this.abs = abs; } @Override public boolean equals(Object obj) { if (obj instanceof DataNode) if (((DataNode) obj).start == this.start) return true; return false; } public int compareTo(Object o) { if (o instanceof DataNode){ if (((DataNode) o).start .compareTo(this.start) == 0) return 0; else if(((DataNode) o).start .compareTo(this.start) < 0) return 1; else return -1; }else return -1; } @Override public String toString() { StringBuffer sb= new StringBuffer(); sb.append("{") .append("start:"+start) .append(",abs:"+abs) .append("}"); return sb.toString(); } } }
测试没有问题 ,可是 出现了 runtime error 百度了 好多人的 ,似乎 也有 这个 问题 ,就是没有给出跟多的提示
知道答案及错误原因请留言,谢谢
捐助开发者
在兴趣的驱动下,写一个免费
的东西,有欣喜,也还有汗水,希望你喜欢我的作品,同时也能支持一下。 当然,有钱捧个钱场(右上角的爱心标志,支持支付宝和PayPal捐助),没钱捧个人场,谢谢各位。
谢谢您的赞助,我会做的更好!
相关推荐
【标题】"POJ1009 - 边缘检测" 在计算机图形学与图像处理领域,边缘检测是一项至关重要的技术。北京大学编程平台POJ上的第1009题,"Edge Detection"(边缘检测),旨在让学生通过编程实现对图像进行边缘检测。此题...
【标题】"POJ.rar_poj java_poj1048" 涉及的知识点主要围绕编程竞赛中的“约瑟夫环”问题,这里是一个加强版,使用Java语言进行解决。 【描述】"POJ1048,加强版的约瑟夫问题 难度中等" 提示我们,这个问题是编程...
总的来说,POJ 1751挑战要求程序员用Prim算法解决求解最小生成树的问题,这涉及到对图论的理解,以及在Java环境中实现和测试该算法的能力。对于想要提升数据结构和算法技能的开发者来说,这是一个很好的实践题目。...
用java的biginteger实现的poj1001,比较简单的方法
总结起来,"凸包练习: POJ 2187(JAVA)"是一个关于几何算法的实际应用问题,通过解决这个问题,你可以学习到如何用JAVA编程语言处理几何问题,掌握凸包算法,以及如何有效地读取、处理和输出数据。同时,这也是提升...
poj1009 Edge Detection 可以直接AC的
【标题】"树状数组练习:POJ 2481(JAVA)" 是一篇关于使用树状数组(也称为线段树)解决编程竞赛问题的文章。这篇文章可能详细讲解了如何运用这种数据结构来解决特定的问题POJ 2481。POJ(Programming Online Judge)...
【北大POJ JAVA源码】是一系列用于解决北京大学在线编程平台(POJ)问题的Java程序集合。这个压缩包中的代码资源,对于学习Java编程、算法设计和优化以及熟悉在线编程竞赛有着重要的参考价值。POJ是北京大学面向全国...
对于POJ-1009,我们需要分析题目需求,选择合适的算法来解决问题。 2. **数据结构**:有效的数据结构可以优化算法的性能,如数组、链表、树、堆、队列、栈等。在处理POJ-1009时,理解何时使用哪种数据结构至关重要...
poj平台有关数据结构题的Java源码 1159 1276 2406 2502 2509 2513 2533 2778 3176
总结来说,解决“字典树练习 POJ 1056”可能需要深入理解字典树的数据结构,实现插入和查询操作,并在Java环境中编写和测试代码。对于初学者,这是一次很好的机会来提高数据结构和算法的应用能力。
在"POJ2002-Squares"这个问题中,参赛者可能需要编写一个程序来处理与正方形相关的数学问题。这可能涉及到计算和处理大量正方形,例如找出一个范围内的所有完全平方数,或者计算不同大小正方形覆盖区域的重叠部分。...
根据给定的信息,本文将对“缙云烧饼 poj openjudge Java”这一题目进行详细的解析,包括题目背景、代码逻辑解读、算法思路分析等方面。 ### 题目背景 题目来源于POJ(Peking University Online Judge)平台上的一...
pku acm 第3356题 AGTC Java代码,有详细的注释,动态规划
提示:二叉树遍历而已,给出前序和中序,求后序 解题思路 1、前序遍历的第一个字母必是 根 2、在中序遍历的字母串中找出 根字母,那么根字母左右两边的字符串就分别是它的左、右子树 3、利用递归复原二叉树(把...
【标题】"POJ1004 - Financial Management" 是一个来自北京大学在线判题系统POJ(Problem Set of Peking University)的编程题目。这个题目主要涉及到计算机科学中的算法设计和实现,特别是与财务管理和计算相关的...
: "POJ解题报告",这个文件很可能是包含所有80道题目解题报告的文档或者多个文档的集合,可能有每个问题的题目描述、解题思路、算法分析、时间复杂度和空间复杂度的讨论,以及C++、Java或Python等语言的源代码示例。...
【标题】"poj 130题 acm pku" 涉及的是ACM(国际大学生程序设计竞赛)中的PKU(北京大学)在线判题系统POJ(Problem Online Judge)的相关题目。ACM/ICPC(International Collegiate Programming Contest)是全球...
标题“PKU_poj 1001~1009”揭示了这是一组与北京大学(Peking University)编程竞赛平台POJ相关的解决方案。在这个压缩包中,包含了从问题1001到1009的C++源代码,表明这些代码已经过验证并成功解决了对应的算法问题。...