神一样的二哥养细菌(难题)

来源:互联网 发布:it it high time that 编辑:程序博客网 时间:2024/05/01 07:52

import java.util.Random;
import java.util.Scanner;

/*
二哥养细菌
题目描述
二哥不仅种苹果和花生,还养了很多细菌。二哥的细菌培养皿成方格形,边长为L。长期培养后,
二哥发现了细菌繁殖的规律:最初每个格子里的细菌及其后代都会独立繁殖,
每次繁殖都会在其上下左右四个相邻的格子里产生新的细菌,而已经存在的细菌在培养皿充满细菌之前都不会死亡。
另外,有一些格子里可能还有抗生素,细菌在有抗生素的格子里无法繁殖。

二哥于是发明了一个游戏:取一个新的培养皿,在某些格子里放入细菌或抗生素,
然后观察细菌不断繁殖直至充满整个培养皿的所有没有抗生素的格子。不过二哥已经对这个游戏厌烦了,
他现在只想知道经过多少轮繁殖后,细菌会充满整个培养皿(不算有抗生素的格子)。

输入格式
第1行有1个整数,边长L。
第2行至第L+1行,每行有L个整数,取值为0、1或2。0表示格子里最初没有细菌,1表示格子里最初有细菌,2表示格子里最初有抗生素。
输出格式
输出一个整数m,表示经过m轮繁殖后,细菌会充满整个培养皿(不算有抗生素的格子)。
说明
【样例解释】 第一轮繁殖:
2 1 0
1 1 1
0 1 0
第二轮繁殖:
2 1 1
1 1 1
1 1 1
【数据范围】
对于全部数据:1≤L≤100 ,保证最终能够充满培养皿(不算有抗生素的格子)。
Sample Input
3
2 0 0
0 1 0
0 0 0
Sample Output
2
*/


/*knowledge: 
n = Math.random(); 返回带正号的 double值,该值大于等于 0.0且小于 1.0。返回值是一个伪随机选择的数,在该范围内(近似)均匀分布。

Random random = new Random();
n = random.nextInt(X); 产生0-(X-1)的随机整数
*/
class Test1{
static Scanner scanner;
static int array1[][],array2[][];
static int size, count, num, a, b, antibiotic = 2, xijun = 1;
static String choice;
static boolean t1 = true, t2 = true;

private static boolean isFull( int array1[][], int size ){
for( int i = 0; i < size; i++ )
for( int j = 0; j < size; j++ )
if( array1[i][j] == 0 )
return false;
return true; 


}

private static void replace( int array1[][], int size ){
array2= new int[size][size];
for( int i = 0; i < size; i++ )
for( int j = 0; j < size; j++ )
array2[i][j] = array1[i][j];
//难点!!!看似简单的东西其实很不简单啊!!
for( int i = 0; i < size; i++ )
for( int j = 0; j < size;j++ )
if( array2[i][j] == 1){
if( i > 0 && array2[i-1][j] == 0 ) 
array1[i-1][j] = 1; 
if( i < size-1 && array2[i+1][j] == 0) 
array1[i+1][j] = 1; 
if( j > 0 && array2[i][j-1] == 0 ) 
array1[i][j-1] = 1; 
if( j < size-1 && array2[i][j+1] == 0 ) 
array1[i][j+1] = 1;
}



}

public static void main(String args[]){
System.out.println("请输入正方形培养皿的大小:");
scanner = new Scanner(System.in);
size = scanner.nextInt(); 
array1= new int[size][size];

//向培养皿中放入抗生素。
while(t1){
System.out.println("输入放入抗生素的位置a b(用空格隔开):");
a = scanner.nextInt();
b = scanner.nextInt();

//判断位置有没有超出培养皿的大小 
if( ( (a-1) < size )&&( (b-1) < size ) ){
array1[a-1][b-1] = antibiotic; 
System.out.println("培养皿"+ a + " " + b +"的位子已经放好抗生素啦!");

}else{
System.out.println("位置超出培养皿大小!");

}

System.out.println("是否还想放入抗生素!y or n");
choice = scanner.next();
/*下面是实现接收char的方法之一要把""改为''没有源代码里简单
choice = scanner.next(); //Scanner中竟然神奇的没有nextChar() next()-->String
char charchoice = choice.charAt(0); //String-->char so输入一定要是单个字符y or n*/

if(choice.equals("y")){

}else if(choice.equals("n")){
t1 = false;
}else{
System.out.println("输入选择有误!"); //?? else之后怎门办? 重新输入怎么办?
}


}

//向培养皿中放细菌
Random random = new Random();
for( int i = 0; i < size; i++ )
for( int j = 0; j < size; j++ ){ 
if( array1[i][j] != antibiotic ){
num = random.nextInt(2); //产生随机数0或1 
array1[i][j] = num;
}

}

//输出培养皿的状况
for( int i = 0; i < size; i++ ){
for( int j = 0; j < size; j++ ){
if( j != (size-1) ){
System.out.print(array1[i][j] + " ");
}else{
System.out.println(array1[i][j]);
}

}

}

//开始繁殖 
while( !isFull(array1, size) ){
replace(array1, size);
count++;
}

System.out.println("繁衍" + count + "次后充满培养皿!");

}

}

/*

结果:

主要问题:


*/