校园赛08长艹问题

时间:2020-04-11
本文章向大家介绍校园赛08长艹问题,主要包括校园赛08长艹问题使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

08、长艹问题

题目

 【问题描述】
 小明有一块空地,他将这块空地划分为 n 行 m 列的小块,每行和每列的长度都为 1。
 小明选了其中的一些小块空地,种上了草,其他小块仍然保持是空地。
 这些草长得很快,每个月,草都会向外长出一些,如果一个小块种了草,则它将向自己的上、下、左、右四小块空地扩展,这四小块空地都将变为有草的小块。
 请告诉小明,k 个月后空地上哪些地方有草。
 【输入格式】
 输入的第一行包含两个整数 n, m。
 接下来 n 行,每行包含 m 个字母,表示初始的空地状态,字母之间没有空格。如果为小数点,表示为空地,如果字母为 g,表示种了草。
 接下来包含一个整数 k。
 【输出格式】
 输出 n 行,每行包含 m 个字母,表示 k 个月后空地的状态。如果为小数点,表示为空地,如果字母为 g,表示长了草。
 【样例输入】
 4 5
 .g...
 .....
 ..g..
 .....
 2
 【样例输出】
 gggg.
 gggg.
 ggggg
 .ggg.
 【评测用例规模与约定】
 对于 30% 的评测用例,2 <= n, m <= 20。
 对于 70% 的评测用例,2 <= n, m <= 100。
 对于所有评测用例,2 <= n, m <= 1000,1 <= k <= 1000。

题目分析和思路:本题又是分为 长草和不长艹,所以我们还是有 二维数组 0 1 来代替,本题我第一次接触dft算法,通过链表实现的队列对输入进行操作,用 i ,j来记录位置,用month来记录月份,很巧妙,还有上下左右移动的操作,定义一个长度为4的数组,-1 和 1进行上下左右传染!

注意再有数字和字符串输入的情况下最好用next() 因为 nextLine() 对Enter敏感,导致输入不了,

  • 代码
  static int N;
    static int M;
    static int K;
    static int[][] sign = new int[1000][1000];
    static int[] line = {1, 0, -1, 0};
    static int[] column = {0, 1, 0, -1};
    static Scanner scanner = new Scanner(System.in);
    static class Block{
        private int x;
        private int y;
        private int month;

        public Block(int x, int y, int month) {
            this.x = x;
            this.y = y;
            this.month = month;
        }
    }
    public static void main(String[] args) throws IOException {
        N = scanner.nextInt();
        M = scanner.nextInt();
        scanner.nextLine();  //干点Enter
        Queue<Block> queue = new LinkedList<>();
        for (int i = 0; i < N; i++) {
            String line = scanner.nextLine();
            for (int j = 0; j < line.length(); j++) {
                if (line.charAt(j) == 'g') {
                    queue.offer(new Block(i, j, 0));
                    sign[i][j] = 1;
                }
            }
        }
        K = scanner.nextInt();
        while (!queue.isEmpty()) {
            Block block = queue.poll();
            int month = block.month;
            if (month < K) {
                for (int i = 0; i < 4; i++) {
                    //向外长草
                    int xx = block.x + line[i];
                    int yy = block.y + column[i];
                    if (xx < N && xx >= 0 && yy >= 0 && yy < M && sign[xx][yy] == 0) {
                        queue.offer(new Block(xx, yy, month + 1));
                        sign[xx][yy] = 1;
                    }
                }
            }
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (sign[i][j] == 1) {
                    writer.write('g');
                } else {
                    writer.write('.');
                }
            }
            writer.write('\n');
        }
        writer.flush();
        writer.close();
    }

总结:算式dft入门吧,输出用了 BufferedWriter进行优化,传染病模型,队列的运用

Scanner输入问题

scanner遇到空格 、Tab、Enter敏感,会直接输入,

再加上nextLine遇到Enter敏感,所以到用到输入数字和字符时不用nextLine 而用next

或者在想执行 nextLine前面加上scanner.nextLine() 干掉前面因为输入数字产出的Enter

  • 比如:
        int a = scanner.nextInt();
        int b = scanner.nextInt();
        String str2 = scanner.nextLine();

我们如果在输入

5 4
hello

就会导致hello输入不了,控制台出现的情况就是不用你按Enter直接给你输出!

原文地址:https://www.cnblogs.com/bingstudy/p/12681318.html