zoukankan      html  css  js  c++  java
  • 藏宝图

    蒜头君得到一张藏宝图。藏宝图是一个 1010×10 的方格地图,图上一共有 1010 个宝藏。有些方格地形太凶险,不能进入。

    整个图只有一个地方可以出入,即是入口也是出口。蒜头君是一个贪心的人,他规划要获得所有宝藏以后才从出口离开。

    藏宝图上从一个方格到相邻的上下左右的方格需要 11 天的时间,蒜头君从入口出发,找到所有宝藏以后,回到出口,最少需要多少天。

    import java.util.Iterator;
    import java.util.Scanner;
    import java.util.Set;
    import java.util.TreeSet;
    
    public class Main {
        static double a;
        static double b;
        static int count;
        static char[][] map = new char[10][10];
        static int[][] book = new int[10][10];
        static Set<Integer> set = new TreeSet<Integer>();
        public static void main(String[] args) {
            Scanner sc = new Scanner(System.in);
            for(int i = 0; i < map.length; i ++) {
                for(int j = 0; j < map[i].length; j ++) {
                    map[i][j] = sc.next().charAt(0);
                }
            }
            a = System.currentTimeMillis();
            dfs(0, 0, 0, 0);
            
            Iterator<Integer> iterator = set.iterator();
            while(iterator.hasNext()) {
                System.out.println(iterator.next());
            }
            
        }
        private static void dfs(int x, int y, int days, int bao) {
            b = System.currentTimeMillis();
            if(b - a > 60000) return;
            
            int[] xx = {-1, 0, 1, 0};
            int[] yy = {0, 1, 0, -1};
            
            if(bao == 10) {
                set.add(days + 1000 * x + 100 * y);
                return;
            }
            
            if(days > 50) return;
            
            for(int i = 0; i < 4; i ++) {
                if(book[x][y] == 0 && x + xx[i] >= 0 && x + xx[i] < 10 && y + yy[i] >= 0 && y + yy[i] < 10 && map[x + xx[i]][y + yy[i]] != 'X') {
                    if(map[x + xx[i]][y + yy[i]] == '#') {
                        book[x][y] = 1;
                        dfs(x + xx[i], y + yy[i], days + 1, bao + 1);
                        book[x][y] = 0;
                    }
                    else {
                        book[x][y] = 1;
                        dfs(x + xx[i], y + yy[i], days + 1, bao);
                        book[x][y] = 0;
                    }
                }
            }
        }
    }
  • 相关阅读:
    json
    封装PDO
    PDO
    jquery练习
    jquery包
    jquery
    租房子 多条件查询
    查询
    新闻修改处理页面
    新闻添加数据
  • 原文地址:https://www.cnblogs.com/jizhidexiaobai/p/8650745.html
Copyright © 2011-2022 走看看