空地长草

空地长草

  • 问题描述
      小明有一块空地,他将这块空地划分为 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。

方法一:

package 十一届;
import java.util.Scanner;
public class _09空地长草 {
	public static int[][] bool;
	public static int[] start;
	public static int[] end;
	public static char[][] num  ;
	public static int k = 0, n = 0, m = 0;
	public static int[] x = { 0, 1, 0, -1 };
	public static int[] y = { 1, 0, -1, 0 };
public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);
		n = sc.nextInt();
		m = sc.nextInt();
		  num = new char[n][m];
		for (int i = 0; i < n; i++) {
			String s = sc.next();
			num[i] = s.toCharArray();
		}
		k = sc.nextInt();
		sc.close();
		start = new int[m * n];
		end = new int[m * n];
		int temp = 0;
		bool = new int[n][m];
		for (int i = 0; i < n; i++) {
			for (int j = 0; j < m; j++) {
				if (num[i][j] == 'g') {
					start[temp] = i;
					end[temp++] = j;
				}
				else{
					bool[i][j]=-1;
				}
			}
		}
		for (int i = 0; i < temp; i++) {
			dfs(start[i],end[i],k);
		}
		for (int i = 0; i < n; i++) {
			for (int j = 0; j <m; j++) {
				System.out.print(num[i][j]);
			}
			System.out.println();
		}
	}

	public static void dfs(int xx, int yy, int kk) {
		
		bool[xx][yy]=kk;
		num[xx][yy]='g';
		for (int i = 0; i < 4; i++) {
			int newx = x[i] + xx;
			int newy = y[i] + yy;
			if ( newx >= 0 && newy >= 0 && newx < n && newy < m&&  kk - 1 > bool[newx][newy]) {
				dfs(newx, newy, kk - 1);
			}
		}
}
}

方法二:

import java.io.*;
import java.util.LinkedList;
import java.util.Scanner;

public class _09空地长草  {
    static final int[] dx = {1, 0, -1, 0};
    static final int[] dy = {0, 1, 0, -1};
    static Scanner sc ;
    static int[][] vis = new int[1000][1000];
    static int N, M, K;

    public static void main(String[] args) throws IOException {
        long now = System.currentTimeMillis();
        //System.setIn(new FileInputStream(new File("***.in")));
        //System.setOut(new PrintStream(new File("***.out")));
        sc = new Scanner(System.in);
        N = sc.nextInt();
        M = sc.nextInt();
        sc.nextLine();
        LinkedList<Block> q = new LinkedList<Block>();
        for (int i = 0; i < N; i++) {
            String line = sc.nextLine();
            for (int j = 0; j < M; j++) {
                if (line.charAt(j) == 'g') {
                    q.addLast(new Block(i, j, 0));
                    vis[i][j] = 1;
                }
            }
        }
        K = sc.nextInt();

        while (!q.isEmpty()) {
            Block b = q.removeFirst();
            int month = b.month;
            if (month < K) {
                for (int i = 0; i <= 3; i++) {
                    int nx = b.i + dx[i];
                    int ny = b.j + dy[i];
                    if (0 <= nx && nx < N && 0 <= ny && ny < M && vis[nx][ny] == 0) {
                        vis[nx][ny] = 1;
                        q.addLast(new Block(nx, ny, month + 1));
                    }
                }
            }
        }
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < M; j++) {
                if (vis[i][j] == 1) writer.write('g');
                else writer.write('.');
            }
            writer.write('\n');
        }
        writer.flush();
        System.err.println(System.currentTimeMillis()-now);
    }

    //草地上的一块
    private static class Block {
        int i;
        int j;
        int month;

        public Block(int i, int j, int month) {
            this.i = i;
            this.j = j;
            this.month = month;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

金石不渝

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值