提交UVA-1665代码至在线判题系统遇Runtime Error求排查
UVA-1665 提交Runtime Error问题排查
我的代码在本地用样例测试正常,但提交到在线判题系统时出现Runtime Error。已经将类名改为public class Main,但问题依旧。以下是我的代码:
import java.io.BufferedReader; import java.io.InputStreamReader; class problemb { static int dx[] = {-1, 0, 1, 0}; static int dy[] = {0, 1, 0, -1}; static int[][] map; static boolean[][] visited; static int n, m; static int t; static int[] years; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int z = Integer.parseInt(br.readLine()); for (int i = 0; i < z; i++) { String[] nm = br.readLine().split(" "); n = Integer.parseInt(nm[0]); m = Integer.parseInt(nm[1]); map = new int[n][m]; visited = new boolean[n][m]; for (int j = 0; j < n; j++) { String[] row = br.readLine().split(" "); for (int k = 0; k < m; k++) { map[j][k] = Integer.parseInt(row[k]); } } t = Integer.parseInt(br.readLine()); years = new int[t]; String[] tValues = br.readLine().split(" "); for (int j = 0; j < t; j++) { years[j] = Integer.parseInt(tValues[j]); } br.close(); int[] results = new int[t]; for (int j = 0; j < t; j++) { int year = years[j]; int areas = 0; // Reset the visited array for the current year visited = new boolean[n][m]; for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) { if (!visited[y][x] && map[y][x] > year) { areas++; bfs(y, x, year); } } } results[j] = areas; } for (int j = 0; j < results.length; j++) { System.out.print(results[j]); if (j < results.length - 1) { System.out.print(" "); } } System.out.println(); } } static void bfs(int y, int x, int year) { int[][] queue = new int[n * m][2]; int head = 0, tail = 0; queue[tail][0] = y; queue[tail][1] = x; tail++; visited[y][x] = true; while (head < tail) { int yy = queue[head][0]; int xx = queue[head][1]; head++; for (int i = 0; i < 4; i++) { int ny = yy + dy[i]; int nx = xx + dx[i]; if (ny < 0 || ny >= n || nx < 0 || nx >= m || visited[ny][nx] || map[ny][nx] <= year) { continue; } visited[ny][nx] = true; queue[tail][0] = ny; queue[tail][1] = nx; tail++; } } } }
问题根源
- 提前关闭输入流:在第一个测试用例处理完后就执行
br.close(),如果测试用例组数z>1,后续读取输入时会触发IO异常,直接导致Runtime Error。 - 输入读取不健壮:
- 使用
split(" ")分割输入时,若遇到连续空格或首尾空格,会生成空字符串,调用Integer.parseInt时抛出异常。 - 假设年份输入都在同一行,但实际判题系统的输入可能将年份拆分为多行,导致读取不完整。
- 使用
修正后的代码
import java.io.BufferedReader; import java.io.InputStreamReader; public class Main { static int dx[] = {-1, 0, 1, 0}; static int dy[] = {0, 1, 0, -1}; static int[][] map; static boolean[][] visited; static int n, m; static int t; static int[] years; public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int z = Integer.parseInt(br.readLine().trim()); for (int i = 0; i < z; i++) { // 读取n和m,跳过空行 String nmLine; do { nmLine = br.readLine().trim(); } while (nmLine.isEmpty()); String[] nm = nmLine.split("\\s+"); n = Integer.parseInt(nm[0]); m = Integer.parseInt(nm[1]); map = new int[n][m]; visited = new boolean[n][m]; for (int j = 0; j < n; j++) { String rowLine; do { rowLine = br.readLine().trim(); } while (rowLine.isEmpty()); String[] row = rowLine.split("\\s+"); for (int k = 0; k < m; k++) { map[j][k] = Integer.parseInt(row[k]); } } // 读取t,跳过空行 String tLine; do { tLine = br.readLine().trim(); } while (tLine.isEmpty()); t = Integer.parseInt(tLine); years = new int[t]; // 读取年份,支持跨多行输入 int count = 0; while (count < t) { String yearLine = br.readLine().trim(); if (yearLine.isEmpty()) continue; String[] tValues = yearLine.split("\\s+"); for (String val : tValues) { if (count >= t) break; years[count++] = Integer.parseInt(val); } } int[] results = new int[t]; for (int j = 0; j < t; j++) { int year = years[j]; int areas = 0; visited = new boolean[n][m]; for (int y = 0; y < n; y++) { for (int x = 0; x < m; x++) { if (!visited[y][x] && map[y][x] > year) { areas++; bfs(y, x, year); } } } results[j] = areas; } for (int j = 0; j < results.length; j++) { System.out.print(results[j]); if (j < results.length - 1) { System.out.print(" "); } } System.out.println(); } br.close(); } static void bfs(int y, int x, int year) { int[][] queue = new int[n * m][2]; int head = 0, tail = 0; queue[tail][0] = y; queue[tail][1] = x; tail++; visited[y][x] = true; while (head < tail) { int yy = queue[head][0]; int xx = queue[head][1]; head++; for (int i = 0; i < 4; i++) { int ny = yy + dy[i]; int nx = xx + dx[i]; if (ny < 0 || ny >= n || nx < 0 || nx >= m || visited[ny][nx] || map[ny][nx] <= year) { continue; } visited[ny][nx] = true; queue[tail][0] = ny; queue[tail][1] = nx; tail++; } } } }
内容的提问来源于stack exchange,提问作者derki
相关产品推荐
相关产品推荐

