Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

poj1009,给出的测试数据过了,时间也不超,但还是Wrong Answer。没想明白哪里错了,求指教

Posted by apocalypse0 at 2017-08-02 17:39:36 on Problem 1009
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

/**
 * POJ-1009-Edge Detection
 */
public class Main{
    public static void main(String[] args){
        Scanner line = new Scanner(System.in);
        int[][] pixIn = new int[1000][2];   //用于保存输入数据
        while(true){
            int width = line.nextInt(); //图像的宽度
            System.out.println(width);
            if(width == 0){
                break;
            }
            int i;
            int k = 0;
            while(true){
                int valeIn = line.nextInt();
                int lenIn = line.nextInt();
                if(valeIn==0 && lenIn==0){
                    break;
                }
                else{   //保存输入数据
                    pixIn[k][0] = valeIn;   //数值
                    pixIn[k][1] = lenIn;    //长度
                    k++;
                }
            }
            int totalNum = 0;   //pixel总数
            for(i = 0; i < k; i++){
                totalNum += pixIn[i][1];
            }

            List<Integer> posit = new ArrayList();  //存储需要计算的位置
            int pn = 0;
            for(i=0; i<k; i++){ //值发生改变的周围都需要计算
                if(!posit.contains(pn)){
                    posit.add(pn);
                }
                if(pn%width-1>0 && pn-1-width>0 && !posit.contains(pn-1-width)){
                    posit.add(pn-1-width);
                }
                if(pn-width>0 && !posit.contains(pn-width)){
                    posit.add(pn-width);
                }
                if((pn+1)%width!=0 && pn+1-width>0 && !posit.contains(pn+1-width)){
                    posit.add(pn+1-width);
                }
                if(pn%width-1>0 && !posit.contains(pn-1)){
                    posit.add(pn-1);
                }
                if((pn+1)%width!=0 && pn+1<totalNum && !posit.contains(pn+1)){
                    posit.add(pn+1);
                }
                if(pn%width-1>0 && pn-1+width<totalNum && !posit.contains(pn-1+width)){
                    posit.add(pn-1+width);
                }
                if(pn+width<totalNum && !posit.contains(pn+width)){
                    posit.add(pn+width);
                }
                if(pn+1+width<totalNum && !posit.contains(pn+1+width)){
                    posit.add(pn+1+width);
                }
                pn += pixIn[i][1];
            }

            Collections.sort(posit);
            int past = -1;
            int count = 0;
            int previ = 0;
            boolean flag = false;
            String outLine = "";    //输出
            for(int index: posit){
                int row = index / width;    //行号(从0开始)
                int col = index % width;    //列号(从0开始)
                int j, m;
                int cur = getValue(row, col, width, totalNum, pixIn, k);    //当前位置的值
                int max = 0;    //新结果处的值
                for(j=row-1; j<=row+1; j++){    //计算新结果处的值
                    for(m=col-1; m<=col+1; m++){
                        int pre = getValue(j, m, width, totalNum, pixIn, k);    //周围数字的值(包括当前位置)
                        if(pre != -1){
                            int tp = Math.abs(cur - pre);   //计算新结果的数值
                            if(tp > max){   //取最大值
                                max = tp;
                            }
                        }
                    }
                }
                if(max != past){
                    if(flag){
                        count = index - previ;
                        System.out.println(outLine+count);
                    }
                    previ = index;
                    outLine = max + " ";
                    past = max;
                }
                flag = true;
            }
            count = totalNum - previ;
            System.out.println(outLine+count);
            System.out.println("0 0");
        }
    }

    /**
     * 获取row行,col列处的值
     * @param row 行号(从0开始)
     * @param col 列号(从0开始)
     * @param width 图像宽度
     * @param totalNum pixel总数
     * @param pixIn 输入的数据(原始数据)
     * @param k 输入数据的行数
     * @return row行,col列处的值
     */
    public static int getValue(int row, int col, int width, int totalNum, int[][]pixIn, int k){
        int index = row * width + col;
        if(row<0 || col<0 || col>width-1 || row>totalNum/width -1 || index>totalNum-1){ //越界的情况
            return -1;
        }
        int sum = 0;
        int i;
        for(i = 0; i < k; i++){ //定位原始数值
            if(sum > index){
                break;
            }
            sum += pixIn[i][1];
        }
        i--;
        return pixIn[i][0];
    }
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator