当前位置:网站首页>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 :
边栏推荐
- MySQL combat optimization expert 12 what does the memory data structure buffer pool look like?
- Security design verification of API interface: ticket, signature, timestamp
- Global and Chinese markets for aprotic solvents 2022-2028: Research Report on technology, participants, trends, market size and share
- [C language] deeply analyze the underlying principle of data storage
- Mysql26 use of performance analysis tools
- MySQL22-逻辑架构
- MySQL实战优化高手07 生产经验:如何对生产环境中的数据库进行360度无死角压测?
- How to build an interface automation testing framework?
- 软件测试工程师必备之软技能:结构化思维
- [untitled]
猜你喜欢
![[reading notes] rewards efficient and privacy preserving federated deep learning](/img/c3/5e88277b5024885d5ceeaa0de14b27.jpg)
[reading notes] rewards efficient and privacy preserving federated deep learning
![[C language] deeply analyze the underlying principle of data storage](/img/d6/1c0cd38c75da0d0cc1df7f36938cfb.png)
[C language] deeply analyze the underlying principle of data storage

Mysql32 lock

软件测试工程师必备之软技能:结构化思维

MySQL实战优化高手11 从数据的增删改开始讲起,回顾一下Buffer Pool在数据库里的地位

MySQL实战优化高手03 用一次数据更新流程,初步了解InnoDB存储引擎的架构设计

MySQL31-MySQL事务日志

How to find the number of daffodils with simple and rough methods in C language

MySQL底层的逻辑架构

如何搭建接口自动化测试框架?
随机推荐
MySQL combat optimization expert 05 production experience: how to plan the database machine configuration in the real production environment?
MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
Emotional classification of 1.6 million comments on LSTM based on pytoch
用于实时端到端文本识别的自适应Bezier曲线网络
How to make shell script executable
Database middleware_ MYCAT summary
First blog
MySQL20-MySQL的数据目录
MySQL30-事务基础知识
Water and rain condition monitoring reservoir water and rain condition online monitoring
软件测试工程师必备之软技能:结构化思维
[C language] deeply analyze the underlying principle of data storage
Introduction tutorial of typescript (dark horse programmer of station B)
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?
Super detailed steps for pushing wechat official account H5 messages
ZABBIX introduction and installation
MySQL ERROR 1040: Too many connections
pytorch的Dataset的使用
ByteTrack: Multi-Object Tracking by Associating Every Detection Box 论文阅读笔记()
Download and installation of QT Creator