当前位置:网站首页>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 :
边栏推荐
- Solution to the problem of cross domain inaccessibility of Chrome browser
- Pytorch RNN actual combat case_ MNIST handwriting font recognition
- API learning of OpenGL (2004) gl_ TEXTURE_ MIN_ FILTER GL_ TEXTURE_ MAG_ FILTER
- ① BOKE
- MySQL storage engine
- What is the current situation of the game industry in the Internet world?
- A necessary soft skill for Software Test Engineers: structured thinking
- Database middleware_ MYCAT summary
- How to change php INI file supports PDO abstraction layer
- Windchill配置远程Oracle数据库连接
猜你喜欢
解决在window中远程连接Linux下的MySQL
用于实时端到端文本识别的自适应Bezier曲线网络
Mysql27 - Optimisation des index et des requêtes
MySQL實戰優化高手04 借著更新語句在InnoDB存儲引擎中的執行流程,聊聊binlog是什麼?
数据库中间件_Mycat总结
MySQL21-用户与权限管理
A necessary soft skill for Software Test Engineers: structured thinking
Use xtrabackup for MySQL database physical backup
Mysql27 index optimization and query optimization
MySQL27-索引优化与查询优化
随机推荐
Database middleware_ MYCAT summary
第一篇博客
Pytorch RNN actual combat case_ MNIST handwriting font recognition
MySQL combat optimization expert 10 production experience: how to deploy visual reporting system for database monitoring system?
保姆级手把手教你用C语言写三子棋
[Julia] exit notes - Serial
Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
ByteTrack: Multi-Object Tracking by Associating Every Detection Box 论文阅读笔记()
Discriminant model: a discriminant model creation framework log linear model
MySQL实战优化高手06 生产经验:互联网公司的生产环境数据库是如何进行性能测试的?
该不会还有人不懂用C语言写扫雷游戏吧
Typescript入门教程(B站黑马程序员)
Use JUnit unit test & transaction usage
Moteur de stockage mysql23
MySQL24-索引的数据结构
MySQL实战优化高手09 生产经验:如何为生产环境中的数据库部署监控系统?
【C语言】深度剖析数据存储的底层原理
Not registered via @EnableConfigurationProperties, marked(@ConfigurationProperties的使用)
In fact, the implementation of current limiting is not complicated
Mysql28 database design specification