解题思路:
这个代码是网上看到一个博主的,有way和cnt的作用不是很清楚,希望看懂的同学可以和我交流一下
注意事项:
参考代码:
import java.util.Scanner; public class 危险系数 { private static int[] vist; private static int[] way; private static int[] cnt; private static int ans = 0; private static int[][] map; private static int n; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); int m = sc.nextInt();// 边数 vist = new int[n + 1]; way = new int[n + 1]; cnt = new int[n + 1]; map = new int[n + 1][n + 1]; for (int i = 0; i < m; i++) { int p1 = sc.nextInt(); int p2 = sc.nextInt(); map[p1][p2] = 1; map[p2][p1] = 1; } int s = sc.nextInt(); int t = sc.nextInt(); dfs(s, t, 0); fun(n); } private static void dfs(int s, int t, int step) { // TODO Auto-generated method stub vist[s] = 1; way[step] = s; if (s == t) { ans++; for (int i = 0; i <= step; i++) { cnt[way[i]]++; } } for (int i = 0; i <= n; i++) { if (map[s][i] == 1 && vist[i] == 0) { vist[i] =1; dfs(i, t, step+1); vist[i] =0; } } } private static void fun(int n) { // TODO Auto-generated method stub int sum = 0; for (int i = 1; i <= n; i++) { if (cnt[i] == ans) { sum++; } // System.out.println("cnt的各个值" + cnt[i]); } System.out.println(sum-2); } }
0.0分
0 人评分
C语言程序设计教程(第三版)课后习题9.4 (C语言代码)浏览:735 |
校门外的树 (C语言代码)浏览:1124 |
C语言训练-排序问题<1> (C语言代码)浏览:1351 |
钟神赛车 (C语言代码)浏览:879 |
简单的a+b (C语言代码)浏览:652 |
简单的a+b (C语言代码)浏览:548 |
C语言训练-素数问题 (C语言代码)浏览:998 |
【蟠桃记】 (C语言代码)浏览:651 |
A+B for Input-Output Practice (VII) (C++代码)浏览:615 |
A+B for Input-Output Practice (C++代码)浏览:609 |