hdu6468 dfs

将数据范围全部转化成一颗十叉树,深度优先遍历情况下第 K 个搜到的就是答案。

import java.util.Scanner;

public class hdu6468_2 {
    static boolean ok;
    static int num = 0;
    public static void dfs(int cur,int aa,int bb){
        if (ok){return;}
        num++;
        if (num == bb){
            System.out.println(cur);
            ok = true;
            return;
        }
        for (int i = 0; i <= 9; i++) {
       int cnt = cur*10+i;
            if (cnt > aa){
                return;
            }
            dfs(cnt ,aa, bb);
        }
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int xx = sc.nextInt();
        for (int ii = 0; ii < xx; ii++) {
            int aa = sc.nextInt();
            int bb = sc.nextInt();
            ok = false;
            num = 0;
            for (int i = 1; i <= 9; i++) {
                dfs(i,aa,bb);
            }
        }
    }
}

 

posted @ 2024-11-20 15:41  XiaohuangTX  阅读(3)  评论(0编辑  收藏  举报