矩阵,烧草的程序

矩阵,有湖,草,
点着草,向四周蔓延,一个小时烧一格,湖不能烧。
求全部烧完的时间。

static char[][] mat = new char[][]{{'G','W','G','G','W','W'},
									{'G','G','G','G','G','W'},
									{'W','G','W','G','W','W'},
									{'G','G','W','G','W','W'},
									{'G','G','G','G','G','G'},
									{'W','W','G','G','W','W'}};
	 
	static int max = 0;
	public static void main(String[] args) {
		
		
		 Shao shao = new Shao();
		 List <String> firstList = shao.fire(mat,5,3);
		 shao.fireList(firstList);
		 
		 
	}
	 
	public void fireList(List<String> fs){
		List<String> nextList = new ArrayList<>();
		for(int i=0;i<fs.size();i++){
			int x = 0;
			int y = 0;
			String pot = fs.get(i);
			String[] pots = pot.split(",");
			x = Integer.valueOf(pots[0]);
			y = Integer.valueOf(pots[1]);
			List<String> thisList = fire(mat,x,y);
			nextList.addAll(thisList);
			
		}
		max++;
		if(nextList.size()>0){
			fireList(nextList);
		}else{
		  System.out.println("max "+max);
		}
	}
	
	 
	
 
	public List<String> fire(char[][] mat , int x, int y ){
		
		List<String> fs = new ArrayList<>();
 
		mat[x][y] = 'F';
		//上
		if((x-1>=0)&&mat[x-1][y]=='G'){
			mat[x-1][y] = 'F';
			fs.add((x-1)+","+y);

		}
	
		//下
		if((x+1)<mat.length && mat[x+1][y]=='G'){
			mat[x+1][y] = 'F';
			fs.add((x+1)+","+y);
		
		}
	
		//左

		if((y-1>=0)&& mat[x][y-1]=='G'){
			mat[x][y-1] = 'F';
			fs.add(x+","+(y-1));
		}
		//右
		if((y+1)<mat.length && mat[x][y+1]=='G'){
			mat[x][y+1] = 'F';
			fs.add(x+","+(y+1));

		}

		return fs;
		
	}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值