AC code:
1. bfs
import javax.swing.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.nio.file.attribute.AclEntryFlag; import java.security.AlgorithmConstraints; import java.sql.Struct; import java.text.CollationElementIterator; import java.text.DateFormatSymbols; import java.util.*; import java.util.stream.Collectors; public class Main { static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static int N = (int)0 + 20; static math_myself math_me = new math_myself(); static char g[][] = new char[N][N]; static boolean used[][] = new boolean[N][N]; static int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1}; static Queue<PII> q = new LinkedList<>(); static int n,m; static int bfs(int x, int y) { q.add(new PII(x,y)); used[x][y] = true; int cnt = 1; // The starting point is the black brick, so it should also be counted while(q. size() > 0) { PII t = q.poll(); for(int i = 0 ; i < 4 ; i ++ ) { int x_cur = t.x + dx[i],y_cur = t.y + dy[i]; if(x_cur < 0 || x_cur >= n || y_cur < 0 || y_cur >= m) continue; // Do not go beyond the boundary if (used[x_cur][y_cur]) continue; // The tiles that have passed will not go if (g[x_cur][y_cur] != '.') continue; // If it is not a brand new black tile, it will not go away // Brand new black tiles must be walkable used[x_cur][y_cur] = true; q.add(new PII(x_cur,y_cur)); cnt + + ; } } return cnt; } public static void main(String[] args ) throws IOException { while(true) { q. clear(); for(int i = 0 ; i < n; i ++ ) Arrays.fill(used[i],false); // Normally, input n first, then m, this question is reversed m = rd.nextInt(); n = rd.nextInt(); if (m == 0 || n == 0) break; // read the matrix for(int i = 0 ; i < n ; i ++ ) g[i] = rd.next().toCharArray(); // find the starting point int x = 0, y = 0, flag = 0; for(int i = 0 ; i < n ; i ++ ) { for(int j = 0 ; j < m ; j ++ ) { if(g[i][j] == '@') { x = i; y = j; flag = 1; } if (flag == 1) break; } } pw. println(bfs(x,y)); } pw.flush(); } } class rd { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String nextLine() throws IOException { return reader. readLine(); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer. nextToken(); } static int nextInt() throws IOException { return Integer. parseInt(next()); } static double nextDouble() throws IOException { return Double. parseDouble(next()); } static long nextLong() throws IOException { return Long. parseLong(next());} static BigInteger nextBigInteger() throws IOException { BigInteger d = new BigInteger(rd. nextLine()); return d; } } class PII { int x,y; public PII(int x ,int y) { this.x = x; this.y = y; } } class math_myself { int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a % b); } int lcm(int a,int b) { return a * b / gcd(a, b); } // Find all divisors of n List get_factor(int n) { List<Long> a = new ArrayList<>(); for(long i = 1; i <= Math. sqrt(n) ; i ++ ) { if(n % i == 0) { a.add(i); if(i != n / i) a.add(n / i); // // Avoid the situation: when x = 16, i = 4, x / i = 4, this will add two situations ^-^How much can the complexity be reduced? } } // Deduplication of the same factor, this method is perfect a = a. stream(). distinct(). collect(Collectors. toList()); // Sort the factors (ascending) Collections. sort(a); return a; } // Check if it is a prime number boolean check_isPrime(int n) { if(n < 2) return false; for(int i = 2 ; i <= n / i; i ++ ) if (n % i == 0) return false; return true; } }
2. dfs
import javax.swing.*; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.math.BigInteger; import java.nio.file.attribute.AclEntryFlag; import java.security.AlgorithmConstraints; import java.sql.Struct; import java.text.CollationElementIterator; import java.text.DateFormatSymbols; import java.util.*; import java.util.stream.Collectors; public class Main { static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out))); static int N = (int)0 + 20; static math_myself math_me = new math_myself(); static char g[][] = new char[N][N]; static int dx[] = {-1,0,1,0},dy[] = {0,1,0,-1}; static int n,m; static int cnt; // Only brand new black tiles will enter dfs static void dfs(int x, int y) { g[x][y] = '#'; // The points that have been passed become red tiles to avoid repeated walking cnt + + ; // the black ones that walked for(int i = 0 ; i < 4 ; i ++ ) { int x_cur = x + dx[i],y_cur = y + dy[i]; if(x_cur < 0 || x_cur >= n || y_cur < 0 || y_cur >= m || g[x_cur][y_cur] == '#') continue; // red tiles or black tiles walked (turns into a red tile) will not enter dfs dfs(x_cur,y_cur); } } public static void main(String[] args ) throws IOException { while(true) { cnt = 0; // Normally, input n first, then m, this question is reversed m = rd.nextInt(); n = rd.nextInt(); if (m == 0 || n == 0) break; // read the matrix for(int i = 0 ; i < n ; i ++ ) g[i] = rd.next().toCharArray(); // find the starting point int x = 0, y = 0, flag = 0; for(int i = 0 ; i < n ; i ++ ) { for(int j = 0 ; j < m ; j ++ ) { if(g[i][j] == '@') { x = i; y = j; flag = 1; } if (flag == 1) break; } } dfs(x,y); // pass in the starting point pw. println(cnt); } pw.flush(); } } class rd { static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); static StringTokenizer tokenizer = new StringTokenizer(""); static String nextLine() throws IOException { return reader. readLine(); } static String next() throws IOException { while (!tokenizer.hasMoreTokens()) tokenizer = new StringTokenizer(reader.readLine()); return tokenizer. nextToken(); } static int nextInt() throws IOException { return Integer. parseInt(next()); } static double nextDouble() throws IOException { return Double. parseDouble(next()); } static long nextLong() throws IOException { return Long. parseLong(next());} static BigInteger nextBigInteger() throws IOException { BigInteger d = new BigInteger(rd. nextLine()); return d; } } class PII { int x,y; public PII(int x ,int y) { this.x = x; this.y = y; } } class math_myself { int gcd(int a, int b) { if(b == 0) return a; else return gcd(b,a % b); } int lcm(int a,int b) { return a * b / gcd(a, b); } // Find all divisors of n List get_factor(int n) { List<Long> a = new ArrayList<>(); for(long i = 1; i <= Math. sqrt(n) ; i ++ ) { if(n % i == 0) { a.add(i); if(i != n / i) a.add(n / i); // // Avoid the situation: when x = 16, i = 4, x / i = 4, this will add two situations ^-^How much can the complexity be reduced? } } // Deduplication of the same factor, this method is perfect a = a. stream(). distinct(). collect(Collectors. toList()); // Sort the factors (ascending) Collections. sort(a); return a; } // Check if it is a prime number boolean check_isPrime(int n) { if(n < 2) return false; for(int i = 2 ; i <= n / i; i ++ ) if (n % i == 0) return false; return true; } }