dfs,这个题没有要求一个最值,可以用dfs,模板题
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(System.out);
static int N = 25, n, m;
static boolean st[][];
static char g[][] = new char[N][N];
static int dx[] = new int[]{1, 0, -1, 0};
static int dy[] = new int[]{0, -1, 0, 1};
public static void main(String[] args) throws Exception {
while (true) {
String s[] = br.readLine().split(" ");
if (s[0].equals("0") || s[1].equals("0")) break;
st = new boolean[N][N];
m = Integer.parseInt(s[0]);
n = Integer.parseInt(s[1]);
int x = 0, y = 0;
for (int i = 0; i < n; i++) {
String ss = br.readLine();
for (int j = 0; j < m; j++) {
g[i][j] = ss.charAt(j);
if (g[i][j] == '@') {
x = i;
y = j;
}
}
}
pw.println(dfs(x, y));
}
pw.flush();
pw.close();
br.close();
}
public static int dfs(int x, int y) {
int cnt = 1;
st[x][y] = true;
for (int i = 0; i < 4; i++) {
int px = x + dx[i];
int py = y + dy[i];
if (px < 0 || px >= n || py < 0 || py >= m) continue;
if (g[px][py] == '@') continue;
if (g[px][py] == '#') continue;
if (st[px][py]) continue;
cnt += dfs(px, py);
}
return cnt;
}
}