poj 1009 java_(需要大神,请求解决,遇见runtime error 错误)poj 1009 java

本地测试 ,没有错误,提交后 runtime error

53925c98d722de432a600d1b5d4f814f.png

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:[email protected]

* @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 target = new ArrayList();

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 source = new TreeSet();

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 target ,Set 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 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 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 百度了 好多人的 ,似乎 也有 这个 问题 ,就是没有给出跟多的提示

知道答案及错误原因请留言,谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值