(Java)Best Grass、城堡问题——用DFS解决

有个愿望:希望自己-38.87摄氏度。

Best Gress:

Bessie is planning her day of munching tender spring grass and is gazing out upon the pasture which Farmer John has so lovingly partitioned into a grid with R (1 <= R <= 100) rows and C (1 <= C <= 100) columns. She wishes to count the number of grass clumps in the pasture. 

Each grass clump is shown on a map as either a single '#' symbol or perhaps two '#' symbols side-by-side (but not on a diagonal) . Given a map of the pasture, tell Bessie how many grass clumps there are. 

By way of example, consider this pasture map where R=5 and C=6: 

.#.... 
..#... 
..#..# 
...##. 
.#....

This pasture has a total of 5 clumps: one on the first row, one that spans the second and third row in column 2, one by itself on the third row, one that spans columns 4 and 5 in row 4, and one more in row 5. 
贝西正计划着她那一天大嚼春天的嫩草,凝视着农场主约翰用R(1<=R<=100)行和C(1<=C<=100)列精心划分成网格的牧场。她想数一下牧场上草丛的数量。
每一个草丛在地图上都显示为一个“#”符号或两个“#”符号并排(但不是在对角线上)。给贝西一张牧场地图,告诉他有多少草丛。
举例来说,考虑这个牧场图,其中R=5和C=6:
.#....

..#...

..#..#

...##.

.#....
这片草地共有5丛:第一行一丛,第二列第二和第三行一丛,第三行一丛,第四行第四和第五列一丛,第五行一丛。
input:
* Line 1: Two space-separated integers: R and C 

* Lines 2..R+1: Line i+1 describes row i of the field with C characters, each of which is a '#' or a '.' 
* 第1行:两个空格分隔的整数:R和C



*第2..R+1行:第i+1行用C个字符描述字段的第i行,每个字符都是“#”或“.”
output:
* Line 1: A single integer that is the number of grass clumps Bessie can munch 
* 第1行:一个整数,表示贝西可以咀嚼的草丛数量
Sample :
5 6 
.#.... 
..#... 
..#..# 
...##. 
.#....
output:
5

在这里插入图片描述



import java.util.Scanner;

public class Main {
	
	static int R,C;
	static int[] x= {1,0,-1,0},y= {0,1,0,-1};//四个方向
	static boolean[][] colors=new boolean[105][105];//默认为false
	static char[][] gresses=new char[105][105];
	static int clumpsNum=0;
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		R=reader.nextInt();
		C=reader.nextInt();
		String e;
		for(int i=0;i<R;i++) {
			e=reader.next();
			gresses[i]=e.toCharArray();//这个方法可将字符串转化为字符数组
		}		
		for(int i=0;i<R;i++)
			for(int k=0;k<C;k++) {
				if(gresses[i][k]=='#') {
					clumpsNum++;
					dfs(i,k);
				}
			}
		System.out.println(clumpsNum);
	
	}
	static void dfs(int i,int k) {
		if(colors[i][k]==true)
			return;
		gresses[i][k]='.';
		colors[i][k]=true;
		for(int t=0;t<4;t++) {
			int xn=i+x[t];
			int yn=k+y[t];
			if(xn>=0&&xn<R&&yn>=0&&yn<C&&gresses[xn][yn]=='#')
				dfs(xn, yn);
		}
		
	
	}

}

城堡问题:


import java.util.Scanner;

public class Main {
	
	static int R,C;
	static int[][] colors=new int[60][60];
	static int[][] rooms=new int[60][60];
	static int MaxroomArea=0,roomArea=0,roomNum;
	public static void main(String[] args) {
		Scanner reader=new Scanner(System.in);
		R=reader.nextInt();
		C=reader.nextInt();
		for(int i=1;i<=R;i++)
			for(int k=1;k<=C;k++)
				rooms[i][k]=reader.nextInt();
		for(int i=1;i<=R;i++)
			for(int k=1;k<=C;k++) {
				if(colors[i][k]==0) {
					roomNum++;
					roomArea=0;
					dfs(i,k);
					MaxroomArea=Math.max(roomArea, MaxroomArea);
					
				}
			}
		System.out.println(roomNum);
		System.out.println(MaxroomArea);
	
	}
	static void dfs(int i,int k) {
		if(colors[i][k]>0||i==0||k==0||i>R||k>C)
			return;
		roomArea++;
		colors[i][k]=roomNum;
		if((rooms[i][k]&1)==0)dfs(i, k-1);
		if((rooms[i][k]&2)==0)dfs(i-1, k);
		if((rooms[i][k]&4)==0)dfs(i, k+1);
		if((rooms[i][k]&8)==0)dfs(i+1, k);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值