京东 王子与公主求问。。。

京东笔试 王子与公主
写出的代码总是报StackOverflow,代码如下,求问哪里出现了问题😫
import java.util.Scanner;

public class Main2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = Integer.parseInt(sc.nextLine());
        for (int i = 0; i < T; i++) {
            String line = sc.nextLine();
            String[] ss = line.split(" ");

            int n = Integer.parseInt(ss[0]);
            int m = Integer.parseInt(ss[1]);
            char[][] table = new char[n][m];
            int[] SPos = new int[2];
            int[] EPos = new int[2];

            for (int j = 0; j < n; j++) {
                String s = sc.nextLine();
                for (int k = 0; k < m; k++) {
                    table[j][k] = s.charAt(k);
                    if (table[j][k] == 'S') {
                        SPos[0] = j;
                        SPos[1] = k;
                    }
                    if (table[j][k] == 'E') {
                        EPos[0] = j;
                        EPos[1] = k;
                    }
                }
            }

            boolean res = dfs(table, SPos, EPos);
            System.out.println(res ? "YES" : "NO");

        }
    }

    private static boolean dfs(char[][] table, int[] SPos, int[] EPos) {
        if (SPos[0] == EPos[0] && SPos[1] == EPos[1]) return true;
        int rows = table.length;
        int cols = table[0].length;
        if (SPos[0] < 0 || SPos[0] >= rows || SPos[1] < 0 || SPos[1] >= cols || table[SPos[0]][SPos[1]] == '#') {
            return false;
        }
        int SR = SPos[0];
        int SL = SPos[1];
        return dfs(table, new int[]{SR - 1, SL}, EPos)
                || dfs(table, new int[]{SR + 1, SL}, EPos)
                || dfs(table, new int[]{SR, SL - 1}, EPos)
                || dfs(table, new int[]{SR, SL + 1}, EPos);
    }
}

#京东##笔试题目#
全部评论
要是不把走过的点设为不能访问的话 他就会一直来回走 递归没终点
1 回复
分享
发布于 2020-09-17 21:16
你弄两个点debug一下就想出来了
点赞 回复
分享
发布于 2020-09-17 21:18
联想
校招火热招聘中
官网直投

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务