矩阵,烧草的程序

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

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;
		
	}


猜你喜欢

转载自blog.csdn.net/os2046/article/details/52743308
今日推荐