解题思路:
这个代码是网上看到一个博主的,有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);
    }

}


点赞(1)
 

0.0分

0 人评分

C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:

一点编程也不会写的:零基础C语言学练课程

解决困扰你多年的C语言疑难杂症特性的C语言进阶课程

从零到写出一个爬虫的Python编程课程

只会语法写不出代码?手把手带你写100个编程真题的编程百练课程

信息学奥赛或C++选手的 必学C++课程

蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程

手把手讲解近五年真题的蓝桥杯辅导课程

评论列表 共有 1 条评论

theking 6年前 回复TA
private static int[] way;    //记录当前这一条路上依次经过的点
	private static int[] cnt;    //记录走过所有路后,点的经过次数