京东 王子与公主求问。。。
京东笔试 王子与公主
写出的代码总是报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); } }