Give me code in java
Tạo vào: 12 tháng 7, 2025
Tạo vào: 12 tháng 7, 2025
pythonfrom PIL import Image import matplotlib.pyplot as plt img = Image.open('/mnt/data/IMG-20250712-WA0011.jpg') plt.imshow(img) plt.axis('off') (-0.5, 1279.5, 719.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img.crop((0,0,800,1200))) plt.axis('off') (-0.5, 799.5, 1199.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img.crop((200,100,700,900))) plt.axis('off') (-0.5, 499.5, 799.5, -0.5)
pythonplt.figure(figsize=(6,10)) plt.imshow(img.crop((150,0,700,450))) plt.axis('off') (-0.5, 549.5, 449.5, -0.5)
Below is a straightforward DFS/back-tracking solution in Java that follows the exact rules in the photo:
javaimport java.io.*; import java.util.*; public class Main { /** graph[u] holds {v, w} pairs where * v = neighbour landmark, w = ticket price */ static List<int[]>[] graph; static boolean[] visited; static int budget; // P static List<Integer> best = new ArrayList<>(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); // #landmarks int M = Integer.parseInt(st.nextToken()); // #bus routes budget = Integer.parseInt(br.readLine().trim()); graph = new ArrayList[N + 1]; for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>(); // build an undirected weighted graph for (int i = 0; i < M; i++) { st = new StringTokenizer(br.readLine()); int u = Integer.parseInt(st.nextToken()); int v = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken()); graph[u].add(new int[]{v, w}); graph[v].add(new int[]{u, w}); } visited = new boolean[N + 1]; List<Integer> path = new ArrayList<>(); visited[1] = true; // start at landmark #1 path.add(1); dfs(1, 0, path); // current landmark, outward-cost so far, path // build the full round-trip: forward then exact reverse (skip duplicate pivot) StringBuilder out = new StringBuilder(); for (int x : best) out.append(x).append(' '); for (int i = best.size() - 2; i >= 0; i--) out.append(best.get(i)).append(' '); System.out.print(out.toString().trim()); } /** depth-first search with backtracking */ private static void dfs(int u, int cost, List<Integer> path) { // record best solution so far if (path.size() > best.size()) best = new ArrayList<>(path); for (int[] edge : graph[u]) { int v = edge[0], w = edge[1]; // only move to new landmarks and stay within budget *including* return if (!visited[v] && (cost + w) * 2 <= budget) { visited[v] = true; path.add(v); dfs(v, cost + w, path); path.remove(path.size() - 1); visited[v] = false; } } } }
(outwardCost + w) * 2 ≤ P
, guaranteeing Julie can pay for the whole round trip.best
.N ≤ 100
, M ≤ 100
. In the worst case the DFS enumerates all simple paths, which is acceptable for these limits.Compile & run in the HirePro editor (or any Java 17+ runtime) and paste the sample/custom input to get the required route printed exactly once on a single line.
Remove comments
javaimport java.io.*; import java.util.*; public class Main { static List<int[]>[] graph; static boolean[] visited; static int budget; static List<Integer> best = new ArrayList<>(); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int N = Integer.parseInt(st.nextToken()); int M = Integer.parseInt(st.nextToken()); budget = Integer.parseInt(br.readLine().trim()); graph = new ArrayList[N + 1]; for (int i = 1; i <= N; i++) graph[i] = new ArrayList<>(); for (int i = 0; i < M; i++) { st = new StringTokenizer(br.readLine()); int u = Integer.parseInt(st.nextToken()); int v = Integer.parseInt(st.nextToken()); int w = Integer.parseInt(st.nextToken()); graph[u].add(new int[]{v, w}); graph[v].add(new int[]{u, w}); } visited = new boolean[N + 1]; List<Integer> path = new ArrayList<>(); visited[1] = true; path.add(1); dfs(1, 0, path); StringBuilder out = new StringBuilder(); for (int x : best) out.append(x).append(' '); for (int i = best.size() - 2; i >= 0; i--) out.append(best.get(i)).append(' '); System.out.print(out.toString().trim()); } private static void dfs(int u, int cost, List<Integer> path) { if (path.size() > best.size()) best = new ArrayList<>(path); for (int[] edge : graph[u]) { int v = edge[0], w = edge[1]; if (!visited[v] && (cost + w) * 2 <= budget) { visited[v] = true; path.add(v); dfs(v, cost + w, path); path.remove(path.size() - 1); visited[v] = false; } } } }