当前位置:网站首页>Li Kou 994: rotten orange (BFS)
Li Kou 994: rotten orange (BFS)
2022-07-29 05:32:00 【Xiao Hong is working hard】
Simple breadth first search (BFS) problem : Store each batch of rotten oranges in a queue . Take out rotten oranges in batches , Take out the rotten orange and put in the fresh rotten orange .
import java.util.LinkedList;
import java.util.Queue;
class Solution {
int[][] dist = {
{
-1,0},{
1,0},{
0,1},{
0,-1}};// Up, down, left and right
public int orangesRotting(int[][] grid) {
int time = 0;// timing
Queue<int[]> queue = new LinkedList<int[]>();
int n = grid.length;
int m = grid[0].length;
boolean[][] booleans = new boolean[n][m];
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(grid[i][j] == 2){
// The first batch of rotten oranges is assigned true, Prevent repeated traversal
queue.offer(new int[]{
i,j});// Rotten oranges join the queue
booleans[i][j] = true;
}
if(grid[i][j] == 0){
// Empty cells are not processed , To prevent traversal, set the value to true
booleans[i][j] = true;
}
}
}
int[] a = new int[2];
while(!queue.isEmpty()){
int num = queue.size();// The number of rotten oranges in each batch
int flag = 0;// If rotten oranges can infect fresh oranges, it will change flag Value .
for(int number = 0;number < num;number++){
a = queue.poll();
for(int i = 0;i < 4;i++){
int x = a[0] + dist[i][0];
int y = a[1] + dist[i][1];
if(x>=0&&y>=0&&x<n&&y<m&&booleans[x][y]!=true){
grid[x][y] = 2;
queue.offer(new int[]{
x,y});
booleans[x][y] = true;
flag = 1;
}
}
}
if(flag == 1){
time++;
}
}
// Traverse all cells , If there are fresh oranges, return -1, Otherwise, return time .
for(int i = 0; i < n;i++){
for(int j = 0;j < m;j++){
if(grid[i][j] == 1){
return -1;
}
}
}
return time;
}
}
边栏推荐
- Application of Huffman tree and Huffman coding in file compression
- C语言数组典型应用代码详细讲解—高手误入(逐步代码详解)
- QML custom tabbar
- 【C语言系列】— 打印100~200之间的素数
- 无重复字符的最长字串
- paddle. Fluid constant calculation error 'nonetype' object has no attribute 'get_ fetch_ list‘
- Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
- Side effects and sequence points
- The road to success in R & D efficiency of 1000 person Internet companies
- Bubble sort c language
猜你喜欢
MySQL的详细安装使用教程(保姆式安装图文讲解)
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
Helm chart for Kubernetes
Day 2
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
科班同学真的了解未来的职业规划吗?
Alibaba cloud Zhang Xintao: heterogeneous computing provides surging power for the digital economy
GPIO的输入输出详解
vim编辑器使用
365 day challenge leetcode1000 question - distance between bus stops on day 038 + time-based key value storage + array closest to the target value after transforming the array and + maximum value at t
随机推荐
365 day challenge leetcode 1000 questions - day 037 elements and the maximum side length of squares less than or equal to the threshold + the number of subsequences that meet the conditions
【活动预告】云上数字工厂与中小企业数字化转型创新论坛
C language file operation
力扣994:腐烂的橘子(BFS)
【C语言系列】— 一道递归小题目
Live broadcast Preview: integration of JD cloud Devops and jfrog product library
Allocate memory: malloc() and free()
Bubble sort c language
AD常用快捷键
Custom QML control: imagebutton
全局components组件注册
C语言 N皇后问题
Day 2
Common shortcut keys for Ad
Occt learning 002 - environment construction
B - 识别浮点常量问题
Thousands of databases, physical machines all over the country, JD logistics full volume cloud live record | interview with excellent technical team
VIM editor use
Introduction to array learning simple question sum of two numbers
More than 200 ISVs have settled in! The first anniversary of Alibaba cloud computing nest