1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| private static void workWay() { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); int c = sc.nextInt(); int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); char[][] graph = new char[n][m]; int startR = 0; int startC = 0; int targetR = 0; int targetC = 0; for (int i = 0; i < n; i++) { String str = sc.nextLine(); graph[i] = str.toCharArray(); for (int j = 0; j < m; j++) { if (graph[i][j] == 'S') { startR = i; startC = j; } else if (graph[i][j] == 'T') { targetR = i; targetC = j; } } } boolean res = findWay(startR, startC, targetR, targetC, graph, t + 1, c, 0); System.out.println(res ? "YES" : "NO"); }
private static boolean findWay(int startR, int startC, int targetR, int targetC, char[][] graph, int t, int c, int direct) { if (startR == targetR && startC == targetC) return true; if (startR < 0 || startC < 0 || startR >= graph.length || startC >= graph[0].length || t < 0) return false; if (graph[startR][startC] == '*') c--; if (c < 0) return false; return findWay(startR - 1, startC, targetR, targetC, graph, direct == 1 ? t : t - 1, c, 1) || findWay(startR, startC - 1, targetR, targetC, graph, direct == 2 ? t : t - 1, c, 2) || findWay(startR + 1, startC, targetR, targetC, graph, direct == 3 ? t : t - 1, c, 3) || findWay(startR, startC + 1, targetR, targetC, graph, direct == 4 ? t : t - 1, c, 4); }
|