当前位置:网站首页>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 :
边栏推荐
- [paper reading notes] - cryptographic analysis of short RSA secret exponents
- Windchill configure remote Oracle database connection
- 基于Pytorch的LSTM实战160万条评论情感分类
- Global and Chinese market for intravenous catheter sets and accessories 2022-2028: Research Report on technology, participants, trends, market size and share
- 数据库中间件_Mycat总结
- Mysql35 master slave replication
- Texttext data enhancement method data argument
- UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd0 in position 0成功解决
- Water and rain condition monitoring reservoir water and rain condition online monitoring
- MySQL28-数据库的设计规范
猜你喜欢
Use JUnit unit test & transaction usage
MySQL combat optimization expert 03 uses a data update process to preliminarily understand the architecture design of InnoDB storage engine
Mysql25 index creation and design principles
API learning of OpenGL (2003) gl_ TEXTURE_ WRAP_ S GL_ TEXTURE_ WRAP_ T
Ueeditor internationalization configuration, supporting Chinese and English switching
Isn't there anyone who doesn't know how to write mine sweeping games in C language
MySQL real battle optimization expert 11 starts with the addition, deletion and modification of data. Review the status of buffer pool in the database
What is the current situation of the game industry in the Internet world?
Not registered via @enableconfigurationproperties, marked (@configurationproperties use)
实现微信公众号H5消息推送的超级详细步骤
随机推荐
MySQL的存储引擎
[reading notes] rewards efficient and privacy preserving federated deep learning
Isn't there anyone who doesn't know how to write mine sweeping games in C language
Just remember Balabala
Mysql25 index creation and design principles
MySQL28-数据库的设计规范
Mysql32 lock
The underlying logical architecture of MySQL
Discriminant model: a discriminant model creation framework log linear model
MySQL实战优化高手02 为了执行SQL语句,你知道MySQL用了什么样的架构设计吗?
使用OVF Tool工具从Esxi 6.7中导出虚拟机
MySQL32-锁
Sed text processing
软件测试工程师发展规划路线
① BOKE
Mysql27 index optimization and query optimization
Database middleware_ MYCAT summary
[unity] simulate jelly effect (with collision) -- tutorial on using jellysprites plug-in
Transactions have four characteristics?
MySQL combat optimization expert 09 production experience: how to deploy a monitoring system for a database in a production environment?