【无标题】有点问题

public class Main {
    private static final int NOT_VISITED = 0;
    private static final int VISITED = 1;
    private static final int VISIT_FINISHED = 2;
    static int[] visitStatus;                          //节点遍历状态
    static List<List<Integer>> edges;                     //记录每个节点的相邻节点
    static boolean stepAllGrids;                      //是否能跳完所有格子的结果判断

    public static boolean step(int N,List<List<Integer>> edges) {
        visitStatus = new int[N];     //节点默认为0 ~ N - 1

        for(int i = 0; i < N && stepAllGrids; i++) {   //总是从节点0开始进入
            if(visitStatus[i] == NOT_VISITED) {
                dfs(i);
            }
        }

        return stepAllGrids;
    }

    public static void dfs(int node) {
        visitStatus[node] = VISITED;

        for(int neighbor : edges.get(node)) {        //遍历相邻节点
            if(visitStatus[neighbor] == NOT_VISITED) {
                dfs(neighbor);

                if(!stepAllGrids) {  //如果退出循环时检测到环,直接退出
                    return;
                }
            }else if(visitStatus[neighbor] == VISITED){   //再一次遍历到此节点,说明存在环,无法完成拓扑排序
                stepAllGrids = false;
                return;
            }
        }

        visitStatus[node] = VISIT_FINISHED;
    }


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while(sc.hasNextLine()) {
            int N = sc.nextInt();
            if(N < 1 || N >= 500) {
                System.out.println("error:输入的格子个数应该大于等于1且小于500");
                return;
            }
            edges = new ArrayList<>();
            for(int i = 0; i < N; i++) {     //默认节点在0 ~ N - 1之间
                edges.add(new ArrayList<>());
            }

            stepAllGrids = true;

            while(sc.hasNext()) {
                int a = sc.nextInt();
                if(a == -1) {            //题目缺失输入退出条件,不妨设置输入-1就退出输入
                    break;
                }
                String line = sc.nextLine();
                String[] strs = line.split(" ");
                if(strs.length != 2) {
                    System.out.println("error:输入的格子数组列数不为2");
                    return;
                }
                if(a < 0 || a >= N) {
                    System.out.println("error:输入的左侧格子号码应该大于等于0且小于" + N);
                    return;
                }
                int b = Integer.parseInt(strs[1]);
                if(b < 0 || b >= N) {
                    System.out.println("error:输入的右侧格子号码应该大于等于0且小于" + N);
                    return;
                }
                edges.get(b).add(a);
            }
            if(step(N, edges)) {
                System.out.println("yes");
            }else {
                System.out.println("no");
            }
        }
    }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值