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

新人刷题,求教大佬们为啥wa啊,,实在查不出来了

Posted by 19930421 at 2023-07-11 15:16:54 on Problem 2251
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Dilaodashi {
    static int i, j, k;

    static char[][][] input;

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            i = scanner.nextInt();
            j = scanner.nextInt();
            k = scanner.nextInt();

            input = new char[i][j][k];
            if (i==0 && j==0 && k==0) {
                break;
            }
            scanner.nextLine();
            for (int l = 0; l < i; l++) {
                for (int m = 0; m < j; m++) {
                    char[] line = scanner.nextLine().toCharArray();
                    for (int n = 0; n < k; n++) {
                        input[l][m][n] = line[n];
                    }
                }
                scanner.nextLine();
            }
            bfs1(input);
        }
    }


    public static void bfs1(char[][][] chars) {
        int step = 0;
        Queue<Node> queue = new LinkedList<Node>();
        int[][][] visit = new int[chars.length][chars[0].length][chars[0][0].length];
        Node start = null;
        for (int l = 0; l < chars.length; l++) {
            for (int m = 0; m < chars[0].length; m++) {
                for (int n = 0; n < chars[0][0].length; n++) {
                    if (chars[l][m][n] == 'S') {
                        start = new Node(l, m, n);
                    }
                }
            }
        }
        queue.offer(start);
        visit[start.ni][start.nj][start.nk] = 1;
        while (!queue.isEmpty()) {
            step++;
            for (int l = 0; l < queue.size(); l++) {
                Node temp = queue.poll();
                if (chars[temp.ni][temp.nj][temp.nk]=='E') {
                    System.out.println("Escaped in " + (step - 1) + " minute(s).");
                    return;
                }

                if (temp.ni > 0 && visit[temp.ni - 1][temp.nj][temp.nk] != 1) {
                    if (chars[temp.ni - 1][temp.nj][temp.nk]!='#') {
                        queue.offer(new Node(temp.ni - 1, temp.nj, temp.nk));
                        visit[temp.ni - 1][temp.nj][temp.nk] = 1;
                    }
                }
                if (temp.ni < chars.length - 1 && visit[temp.ni + 1][temp.nj][temp.nk] != 1) {
                    if (chars[temp.ni + 1][temp.nj][temp.nk]!='#') {
                        queue.offer(new Node(temp.ni + 1, temp.nj, temp.nk));
                        visit[temp.ni + 1][temp.nj][temp.nk] = 1;
                    }

                }
                if (temp.nj > 0 && visit[temp.ni][temp.nj - 1][temp.nk] != 1) {
                    if (chars[temp.ni][temp.nj - 1][temp.nk]!='#') {
                        queue.offer(new Node(temp.ni, temp.nj - 1, temp.nk));
                        visit[temp.ni][temp.nj - 1][temp.nk] = 1;
                    }
                }
                if (temp.nj < chars[0].length - 1 && visit[temp.ni][temp.nj + 1][temp.nk] != 1) {
                    if (chars[temp.ni][temp.nj + 1][temp.nk] !='#') {
                        queue.offer(new Node(temp.ni, temp.nj + 1, temp.nk));
                        visit[temp.ni][temp.nj + 1][temp.nk] = 1;
                    }
                }
                if (temp.nk > 0 && visit[temp.ni][temp.nj][temp.nk - 1] != 1) {
                    if (chars[temp.ni][temp.nj][temp.nk - 1] != '#') {
                        queue.offer(new Node(temp.ni, temp.nj, temp.nk - 1));
                        visit[temp.ni][temp.nj][temp.nk - 1] = 1;
                    }
                }
                if (temp.nk < chars[0][0].length - 1 && visit[temp.ni][temp.nj][temp.nk + 1] != 1) {
                    if (chars[temp.ni][temp.nj][temp.nk + 1] != '#') {
                        queue.offer(new Node(temp.ni, temp.nj, temp.nk + 1));
                        visit[temp.ni][temp.nj][temp.nk + 1] = 1;
                    }
                }
            }
        }
        System.out.println("Trapped!");
    }

    static class Node {
        int ni;

        int nj;

        int nk;

        public Node() {
        }

        public Node(int ni, int nj, int nk) {
            this.ni = ni;
            this.nj = nj;
            this.nk = nk;
        }
    }
}

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