代码随想三刷图论篇2
创始人
2025-01-10 12:38:02
0

代码随想三刷图论篇2

  • 104. 建造最大岛屿
    • 题目
    • 代码
  • 110. 字符串接龙
    • 题目
    • 代码
  • 105. 有向图的完全可达性
    • 题目
    • 代码
  • 106. 岛屿的周长
    • 题目
    • 代码

104. 建造最大岛屿

题目

链接

代码

import java.util.*; class Main{          public static void main(String[] args){         Scanner sc = new Scanner(System.in);         int N = sc.nextInt();//行         int M = sc.nextInt();//列         int[][] pic = new int[N][M];         used = new boolean[N][M];         for(int i =0;i             for(int j =0;j                 pic[i][j] = sc.nextInt();                 if(pic[i][j]==0){                     used[i][j] = true;                 }             }         }         int maxCount = 0;         int index = 1;         Map map = new HashMap();          for(int i =0;i             for(int j =0;j                 if(pic[i][j]==1){                     list.clear();                     dfs(pic,i,j);                     for(int k = 0;k                         int[] temp= list.get(k);                         pic[temp[0]][temp[1]] = index;                     }                     map.put(index,list.size());                     maxCount = Math.max(maxCount,list.size());                     index++;                 }             }         }         for(int i =0;i             for(int j =0;j                 if(pic[i][j]==0){                     Set sett = new HashSet();                     int count = 1;                     if(i>=1 && !sett.contains(pic[i-1][j])){                         count+=map.getOrDefault(pic[i-1][j],0);                         sett.add(pic[i-1][j]);                     }                     if(j>=1 && !sett.contains(pic[i][j-1])){                         count+=map.getOrDefault(pic[i][j-1],0);                         sett.add(pic[i][j-1]);                     }                     if(i                         count += map.getOrDefault(pic[i+1][j],0);                         sett.add(pic[i+1][j]);                     }                     if(j                         count+=map.getOrDefault(pic[i][j+1],0);                         sett.add(pic[i][j+1]);                     }                     maxCount = Math.max(maxCount,count);                 }             }         }         System.out.println(maxCount);              }     static boolean[][] used;     static List list = new ArrayList();     public static void dfs(int[][] pic,int i,int j){         if(i<0||j<0||i>=pic.length||j>=pic[0].length||used[i][j]){             return;         }         used[i][j] = true;         list.add(new int[]{i,j});         if(i>=1){             dfs(pic,i-1,j);         }         if(j>=1){             dfs(pic,i,j-1);         }         if(i             dfs(pic,i+1,j);         }         if(j             dfs(pic,i,j+1);         }     } } 

110. 字符串接龙

题目

链接

代码

import java.util.*;  class Main{     public static void main(String[] args){         Scanner sc = new Scanner(System.in);                  int N = Integer.valueOf(sc.nextLine());         String beginStr = sc.next();         String endStr = sc.next();         Set strList = new HashSet();         for(int i = 0;i             strList.add(sc.next());         }         int count = 0;         Deque  queue = new LinkedList();         queue.addLast(beginStr);         while(!queue.isEmpty()){             count++;             int size = queue.size();             for(int step = 0;step                 String word = queue.removeFirst();                 for(int i=0;i< word.length();i++){                                      char[] chars = word.toCharArray();                     for(char k = 'a';k<='z';k++){                         chars[i] = k;                            String newWord = String.valueOf(chars);                         if(newWord.equals(endStr)){                             System.out.println(count+1);                             return;                         }                         if(strList.contains(newWord)){                             strList.remove(newWord);                             queue.addLast(newWord);                         }                     }                 }             }         }                  System.out.println(0);     } } 

105. 有向图的完全可达性

题目

链接

代码

import java.util.*;  class Main{          public static void main(String[] args){         Scanner sc = new Scanner(System.in);         int N = sc.nextInt();         int K = sc.nextInt();         Map> map = new HashMap();         for(int i =1;i<=N;i++){             map.put(i,new HashSet());         }         for(int i = 0;i             int num1 = sc.nextInt();             int num2 = sc.nextInt();             map.get(num1).add(num2);         }         used = new boolean[N+1];         dfs(map,1);         for(int i =1;i<=N;i++){             if(!used[i]){                 System.out.println(-1);                 return;             }         }         System.out.println(1);     }     static boolean[] used;     public static void dfs(Map> map,int key){         if(used[key]){             return;         }         used[key] = true;         Set set =map.get(key);         for(int nextNum:set){             dfs(map,nextNum);         }     } } 

106. 岛屿的周长

题目

链接

代码

import java.util.*;  class Main{     public static void main(String[] args){         Scanner sc = new Scanner(System.in);         int N = sc.nextInt();         int M = sc.nextInt();                  int[][] pic = new int[N][M];         for(int i=0;i             for(int j=0;j                 pic[i][j] = sc.nextInt();             }         }                  int sum = 0;    // 陆地数量         int cover = 0;  // 相邻数量         for (int i = 0; i < N; i++) {             for (int j = 0; j < M; j++) {                 if (pic[i][j] == 1) {                     sum++; // 统计总的陆地数量                     // 统计上边相邻陆地                     if(i - 1 >= 0 && pic[i - 1][j] == 1) cover++;                     // 统计左边相邻陆地                     if(j - 1 >= 0 && pic[i][j - 1] == 1) cover++;                     // 为什么没统计下边和右边? 因为避免重复计算                 }             }         }         System.out.println(sum * 4 - cover * 2 );     } } 

相关内容

热门资讯

wepower有外挂!hhpo... wepower有外挂!hhpoker德州有挂(透视)存在挂教程(有挂猫腻)-哔哩哔哩;1、超多福利:...
wepokeai机器人!wej... wepokeai机器人!wejoker透视方法(透视)细节揭秘(确实有挂)-哔哩哔哩;亲真的是有正版...
wepokeai代打的胜率!p... wepokeai代打的胜率!pokermaster脚本(透视)黑科技教程(有挂秘笈)-哔哩哔哩是一款...
wepoke黑科技!德普之星透... wepoke黑科技!德普之星透视辅助软件是真的(透视)教你攻略(有挂方略)-哔哩哔哩是一款可以让一直...
wepoke是真的有挂!hh ... wepoke是真的有挂!hh poker辅助有用(透视)细节方法(有挂功能)-哔哩哔哩;最新版202...
wepokeai代打逻辑!po... wepokeai代打逻辑!pokemmo手机版脚本免费(透视)透明教程(真的有挂)-哔哩哔哩;是一款...
wepoke有app软件!we... wepoke有app软件!wepoker辅助工具(透视)透牌教程(有挂分析)-哔哩哔哩;wepoke...
wepok软件透明挂!哈糖大菠... 【福星临门,好运相随】;wepok软件透明挂!哈糖大菠萝软件下载(透视)2025新版教程(揭秘有挂)...
七分钟了解!新畅游互娱科技(辅... 七分钟了解!新畅游互娱科技(辅助挂)详细透视开挂辅助新2025版(切实真的有挂)-哔哩哔哩;1、首先...
wepokeai代打逻辑!德州... wepokeai代打逻辑!德州hhpoker脚本(透视)辅助教程(有挂秘笈)-哔哩哔哩;是一款可以让...