求连通块的数量(dfs、bfs)
生活随笔
收集整理的這篇文章主要介紹了
求连通块的数量(dfs、bfs)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
AC代碼:
1、bfs
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;// 只有嶄新的黑色瓷磚才會進dfsstatic void dfs(int x, int y){g[x][y] = '#'; // 走過的點變成紅色瓷磚,以免重復走cnt ++; // 走過的黑色的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; // 紅色的瓷磚或者走過的黑瓷磚(變成紅色瓷磚)不會進dfsdfs(x_cur,y_cur);}}public static void main(String[] args ) throws IOException{while(true){cnt = 0;// 正常是先輸入n,再輸入m,這題反著m = rd.nextInt();n = rd.nextInt();if (m == 0 || n == 0) break;// 讀入矩陣for(int i = 0 ; i < n ; i ++) g[i] = rd.next().toCharArray();// 尋找起點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); // 傳入起點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);}// 求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); // // 避免一下的情況:x = 16時,i = 4 ,x / i = 4的情況,這樣會加入兩種情況 ^-^復雜度能減少多少是多少}}// 相同因子去重,這個方法,完美a = a.stream().distinct().collect(Collectors.toList());// 對因子排序(升序)Collections.sort(a);return a;}// 判斷是否是質數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;} }總結
以上是生活随笔為你收集整理的求连通块的数量(dfs、bfs)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: xbox会员中心在哪_白金会会员中心
- 下一篇: 揭秘阿里员工每天必刷的内网