提交时间:2024-03-13 21:09:24
运行 ID: 43407
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); } }