2020年模拟--种树(dfs)

88 篇文章 1 订阅

题目

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

有问题,这个要看最终官方给的数据

import java.util.Scanner;

public class 种草 {
	public static int[] x={0,1,0,-1};//横坐标的方向
	public static int[] y={1,0,-1,0};//纵坐标的方向
	public static int n=0,m=0,k=0;
	public static int[][] bool;//相当于是否染色,这里就是是否走过
	public static int[] start;//起始状态
	public static int[] end;//终止状态
	public static char[][] a;//存放的标志
	
	//深搜题目,xx表示横坐标,yy表示纵坐标,kk表示要几个月
	public static void dfs(int xx,int yy,int kk){
		bool[xx][yy]=kk;//表示在k这个月,这个地方走过了
		a[xx][yy]='g';//表示此地方已经种上草了
		for(int i=0;i<4;i++){//方向有4个方向
			int newx=x[i]+xx;//新的横坐标进行前进
			int newy=y[i]+xx;//新的纵坐标进行前进
			//因为x,y交错的,所以每输出异步就是不同的
			if(newx>=0&&newy>=0&&newx<n&&newy<m&&kk-1>bool[newx][newy]){
				dfs(newx,newy,kk-1);
			}
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		m=sc.nextInt();
		a=new char[n][m];
		for(int i=0;i<n;i++){
			String s=sc.next();
			a[i]=s.toCharArray();
		}
		k=sc.nextInt();//表示要几个月
		sc.close();
		start=new int[n*m];//开始的状态
		end=new int[n*m];//结束的状态
		int temp=0;
		bool =new int[n][m];//是否走过
		for(int i=0;i<n;i++){
			for(int j=0;j<m;j++){
				if(a[i][j]=='g'){//如果此时种了草
					start[temp]=i;//开始横坐标为i,表示(i,j)种了草
					//System.out.println("temp="+temp);
					end[temp++]=j;
					//System.out.println("temp1="+temp);
					//System.out.println("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(a[i][j]);
			}
			System.out.println();
		}
	}

}

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class 种树1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,m,month;
		ArrayList<Node> node=new ArrayList<Node>();
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		m=sc.nextInt();
		sc.nextLine();//消除符号
		int num[][]=new int[n][m];
		for(int i=0;i<n;i++){
			String s=sc.next();
			for(int j=0;j<m;j++){
				if(s.charAt(j)=='.')
					num[i][j]=0;
				else{
					num[i][j]=1;
					node.add(new Node(i,j));//添加有草的坐标,代表一个节点
				}
			}
		}
		month=sc.nextInt();
		bfs(num,node,month,m,n);
	}
	private static void bfs(int[][] num, ArrayList<Node> node, int month, int m, int n) {
		// TODO Auto-generated method stub
		int[][] direct = {{-1,0},{1,0},{0,-1},{0,1}};
		//队列
		Queue<Node>queue=new LinkedList<Node>();
		while(month>=0){
			for(int i=0;i<node.size();i++){
				queue.offer(node.get(i));//放入多个起点
			}
			while(queue.size()>0){
				Node temp=queue.poll();//拿出队头元素
				for(int i=0;i<4;i++){
					int tx=temp.x+direct[i][0];
					int ty=temp.y+direct[i][1];
					if(tx<0||ty<0||tx>m-1||ty>n-1)
						continue;
					if(num[ty][tx]==1)//当已经有草的时候
						continue;
					Node next=new Node(tx,ty);//添加了next节点
					num[ty][tx]=1;//标记为1
					node.add(new Node(ty,tx));
				}
				month--;
			}
		}
		int i,k;
		for(i=0;i<num.length;i++){
			for(k=0;k<num[i].length;k++){
				if(num[i][k]==1)
					System.out.print('g');
				else
					System.out.println('.');
			}
			System.out.println();
		}
	}

}


class Node{//节点
	int x;//起点
	int y;//终点
	Node(int x,int y){
		this.x=x;
		this.y=y;
	}
}

在这里插入图片描述

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.LinkedList;
import java.util.Scanner;

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

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		sc=new Scanner(System.in);
		N = sc.nextInt();//n行
		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
                    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();        
	}
    //草地上的一块
    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;
        }
    }
}

这个是我修改了一下我发的前几个的数据,把arralylist改为了linkedlist就可以用了


import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class 种树1 {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int n,m,month;
		LinkedList<Node> node=new LinkedList<Node>();
		Scanner sc=new Scanner(System.in);
		n=sc.nextInt();
		m=sc.nextInt();
		sc.nextLine();//消除符号
		int num[][]=new int[n][m];
		for(int i=0;i<n;i++){
			String s=sc.next();
			for(int j=0;j<m;j++){
				if(s.charAt(j)=='.')
					num[i][j]=0;
				else{
					num[i][j]=1;
					node.add(new Node(i,j));//添加有草的坐标,代表一个节点
				}
			}
		}
		month=sc.nextInt();
		bfs(num,node,month,m,n);
	}
	private static void bfs(int[][] num, LinkedList<Node> node, int month, int m, int n) {
		// TODO Auto-generated method stub
		int[][] direct = {{-1,0},{1,0},{0,-1},{0,1}};
		//队列
		Queue<Node>queue=new LinkedList<Node>();
		while(month>=0){
			for(int i=0;i<node.size();i++){
				queue.offer(node.get(i));//放入多个起点
			}
			while(queue.size()>0){
				Node temp=queue.poll();//拿出队头元素
				for(int i=0;i<4;i++){
					int tx=temp.x+direct[i][0];
					int ty=temp.y+direct[i][1];
					if(tx<0||ty<0||tx>m-1||ty>n-1)
						continue;
					if(num[ty][tx]==1)//当已经有草的时候
						continue;
					Node next=new Node(tx,ty);//添加了next节点
					num[ty][tx]=1;//标记为1
					node.add(new Node(ty,tx));
				}
				month--;
			}
		}
		int i,k;
		for(i=0;i<num.length;i++){
			for(k=0;k<num[i].length;k++){
				if(num[i][k]==1)
					System.out.print('g');
				else
					System.out.print('.');
			}
			System.out.println();
		}
	}

}


class Node{//节点
	int x;//起点
	int y;//终点
	Node(int x,int y){
		this.x=x;
		this.y=y;
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

向上Claire

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

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

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

打赏作者

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

抵扣说明:

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

余额充值