当前位置:网站首页>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;
}
}
边栏推荐
- VIM editor use
- 365 day challenge leetcode 1000 questions - day 042 array sequence number conversion + relative ranking discretization processing
- 分配内存:malloc()和free()
- Day 2
- C语言 N皇后问题
- JD cloud golden autumn cloud special offer is in progress! Code scanning participation activities
- C语言 一维数组
- With cloud simulation platform, Shichuang technology supports the upgrading of "China smart manufacturing"
- PyQt5:第一章第1节:使用Qt组件创建一个用户界面-介绍
- 321, Jingdong Yanxi × Nlpcc 2022 challenge starts!
猜你喜欢

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

vim编辑器使用

【C语言系列】— 打印100~200之间的素数

In depth analysis of common cross end technology stacks of app

Best practices of JD cloud Distributed Link Tracking in financial scenarios

Live broadcast preview | how to save 30% labor cost and shorten 80% trademark processing cycle?

C语言数组入门到精通(数组精讲)

Alibaba cloud architect Liang Xu: MES on cloud box helps customers quickly build digital factories

浅谈范式

C language first level pointer
随机推荐
C语言 N皇后问题
Integer overflow and printing
Differences between texture2d and texture2dproj under webgl1.0
终端shell常用命令
C语言数组入门到精通(数组精讲)
CMU15-213 Shell Lab实验记录
Pyqt5: Chapter 1, Section 1: creating a user interface using QT components - Introduction
Detailed explanation of GPIO input and output
With frequent data leakage and deletion events, how should enterprises build a security defense line?
Come on! See how Clickhouse, which has risen 16 places a year, can be implemented in jd.com
Custom QML control: imagebutton
Best practices of JD cloud Distributed Link Tracking in financial scenarios
【剑指offer】— 详解库函数atoi以及模拟实现atoi函数
平行云CEO 李岩:CloudXR ,开启通往元宇宙的通道
C language n queen problem
MySQL的详细安装使用教程(保姆式安装图文讲解)
D3d Shader Instruction
一维数组练习
Preemptive appointment | Alibaba cloud shadowless cloud application online conference appointment opens
Bubble sort c language