Run ID | 作者 | 问题 | 语言 | 测评结果 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|
43407 | 老方 | 红与黑 | Java | 运行出错 | 158 MS | 16420 KB | 1281 | 2024-03-13 21:09:24 |
import java.util.Scanner; public class Main { char[][] ch; int k, n, m, x1, y3, x2, y2; int[] dx = {0, -1, 0, 1}; int[] dy = {-1, 0, 1, 0}; int cnt = 1; boolean flag = false; public void dfs(int x, int y) { for (int i = 0; i < 4; i++) { int x1 = x + dx[i]; int y3 = y + dy[i]; if (x1 >= 0 && x1 < m && y3 >= 0 && y3 < n && ch[x1][y3] != '#') { ch[x1][y3] = '#'; cnt++; dfs(x1, y3); } } } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); Main solver = new Main(); solver.n = scanner.nextInt(); solver.m = scanner.nextInt(); solver.ch = new char[solver.m][solver.n]; for (int i = 0; i < solver.m; i++) { for (int j = 0; j < solver.n; j++) { solver.ch[i][j] = scanner.next().charAt(0); if (solver.ch[i][j] == '@') { solver.x1 = i; solver.y3 = j; solver.ch[i][j] = '#'; } } } solver.dfs(solver.x1, solver.y3); System.out.println(solver.cnt); } }