在Spoj中提交我的Java代码时出现问题

时间:2018-11-20 13:49:16

标签: java

我尝试了各种测试用例,并且一切都在Eclipse中运行。但是在提交时显示NZEC错误。任何人都可以告诉我如何纠正此问题或问题所在。距我开始编码仅三个星期,我发现很难找到运行时发生的错误。

import java.util.Scanner;
class Main {
    public static void main(String[] args) {

    Scanner alpha= new Scanner(System.in);
    int n= alpha.nextInt();
    int m= alpha.nextInt();
    int[] s1=new int[Math.min(n, m)];
    int[] s2=new int[Math.min(n, m)];
    int count=0;
    int l= alpha.nextInt();
    String[][] maze= new String[n][m];
    for(int i=0;i<n;i++) {
        char[] c= alpha.next().toCharArray();
        for(int j=0;j<m;j++) {
            maze[i][j]=String.valueOf(c[j]);
            if(maze[i][j].equals("@")) {
            s1[count]=i;
            s2[count++]=j;
            }
        }
    }
    alpha.close();
    boolean flag= false;
    int i=0;
    while(i<count) {
        int entry1=s1[i];
        int entry2=s2[i];
        i++;
        flag= process(entry1,entry2,l,maze,0);
        if(flag)
            break;
    }
    System.out.println(flag?"SUCCESS":"IMPOSSIBLE");
}

public static boolean isSafe(int a, int b, String[][]maze) {
    return (a<maze.length && b<maze[0].length && !maze[a][b].equals("#")); 
}
public static boolean process(int a,int b,int c,String[][]maze,int d){
    if(c>=d &&((a+1<maze.length && maze[a+1][b].equals("x"))||(b+1<maze[0].length && maze[a][b+1].equals("x"))||(b-1>=0 && maze[a][b-1].equals("x"))||(a-1>=0 && maze[a-1][b].equals("x"))))
        return true;
    if(isSafe(a+1,b,maze)) {
        if(maze[a+1][b].equals("s"))
        {
            if( c>0)
                return process(a+1,b,c-1,maze,d+1);
            else
                return false;
        }
        if(maze[a+1][b].equals("."))
            return process(a+1,b,c,maze,d);
    }
    if(isSafe(a,b+1,maze)) {
        if(maze[a][b+1].equals("s"))
        {
            if( c>0)
                return process(a,b+1,c-1,maze,d+1);
            else
                return false;
        }
        if(maze[a][b+1].equals("."))
            return process(a,b+1,c,maze,d);
    }
    if(isSafe(a,b-1,maze)) {
        if(maze[a][b-1].equals("s"))
        {
            if( c>0)
                return process(a,b-1,c-1,maze,d+1);
            else
                return false;
        }
        if(maze[a][b-1].equals("."))
            return process(a,b-1,c,maze,d);
    }
    if(isSafe(a-1,b,maze)) {
        if(maze[a-1][b].equals("s"))
        {
            if( c>0)
                return process(a-1,b,c-1,maze,d+1);
            else
                return false;
        }
        if(maze[a-1][b].equals("."))
            return process(a-1,b,c,maze,d);
    }

return false;
}   
}

请参考以下代码 问题的链接是https://www.spoj.com/problems/SPIKES/

0 个答案:

没有答案