当前位置:网站首页>Breadth first search rotten orange
Breadth first search rotten orange
2022-07-06 10:38:00 【GUTSZ】
Breadth first search rotten orange
problem :
Code :
class pair{
int x;
int y;
public pair(int x,int y){
this.x=x;
this.y=y;
}
}
class Solution {
public int orangesRotting(int[][] grid) {
if (grid.length == 0)
return 0;
int row = grid.length;
int col = grid[0].length;
Queue<pair> q = new LinkedList<>();
// Find the bad fruit , The team
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 2)
q.offer(new pair(i, j));
}
}
int[][] nextp = {
{
1, 0}, {
-1, 0}, {
0, 1}, {
0, -1}};
int step = 0;
while (!q.isEmpty()) {
int size = q.size();
boolean flag = false;
while (size-- != 0) {
pair cur = q.poll();
// Search for a new location
for (int i = 0; i < 4; i++) {
int nx = cur.x + nextp[i][0];
int ny = cur.y + nextp[i][1];
if (nx >= row || nx < 0 || ny >= col || ny < 0)
continue;
if (grid[nx][ny] == 1) {
flag = true;
grid[nx][ny] = 2;
q.offer(new pair(nx, ny));
}
}
}
if (flag)
step++;
}
for (int i = 0; i < row; i++) {
for (int j = 0; j < col; j++) {
if (grid[i][j] == 1)
return -1;
}
}
return step;
}
}
Running results :
边栏推荐
- [programmers' English growth path] English learning serial one (verb general tense)
- MySQL combat optimization expert 02 in order to execute SQL statements, do you know what kind of architectural design MySQL uses?
- MySQL25-索引的创建与设计原则
- 16 medical registration system_ [order by appointment]
- MySQL23-存儲引擎
- February 13, 2022 - Maximum subarray and
- ByteTrack: Multi-Object Tracking by Associating Every Detection Box 论文阅读笔记()
- Implement context manager through with
- Pytoch LSTM implementation process (visual version)
- Typescript入门教程(B站黑马程序员)
猜你喜欢

MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database

MySQL35-主从复制
![[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in](/img/1f/93a6c6150ec2b002f667a882569b7b.jpg)
[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in

Download and installation of QT Creator

Pytoch LSTM implementation process (visual version)

MySQL23-存儲引擎

MySQL33-多版本并发控制

使用OVF Tool工具从Esxi 6.7中导出虚拟机

基于Pytorch的LSTM实战160万条评论情感分类

Isn't there anyone who doesn't know how to write mine sweeping games in C language
随机推荐
好博客好资料记录链接
C language string function summary
[paper reading notes] - cryptographic analysis of short RSA secret exponents
Use JUnit unit test & transaction usage
UEditor国际化配置,支持中英文切换
Download and installation of QT Creator
Just remember Balabala
MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
Implement sending post request with form data parameter
Pytoch LSTM implementation process (visual version)
MySQL22-逻辑架构
Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
How to change php INI file supports PDO abstraction layer
MySQL26-性能分析工具的使用
MySQL combat optimization expert 06 production experience: how does the production environment database of Internet companies conduct performance testing?
Solution to the problem of cross domain inaccessibility of Chrome browser
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
数据库中间件_Mycat总结