leetcode994
public class Solution
{
int row = ;
int column = ;
int FreshOrangeCount = ;
int RottenOrangeCount = ;
int Minute = ;
Queue<int[]> Q = new Queue<int[]>();
int[,] TagGrid;
public void BFS(int[][] grid)
{
var rottinglist = new List<int[]>(); while(Q.Count>)
{
while (Q.Count > )
{
var orange = Q.Dequeue(); int i = orange[];
int j = orange[]; int i_new = -;
int j_new = -; //合法范围:up,down,left,right
//未访问的:TagGrid[newi,newj]==0
//新鲜的(grid[i][j]==1)
i_new = i - ;
j_new = j; //up:i-1>=0
if (i_new >= && TagGrid[i_new, j_new] == && grid[i_new][j_new] == )
{
FreshOrangeCount--;
RottenOrangeCount++;
TagGrid[i_new, j_new] = ;//visited
grid[i_new][j_new] = ;//go bad
rottinglist.Add(new int[] { i_new, j_new });
} //down:i+1<=row-1
i_new = i + ;
j_new = j;
if (i_new <= row - && TagGrid[i_new, j_new] == && grid[i_new][j_new] == )
{
FreshOrangeCount--;
RottenOrangeCount++;
TagGrid[i_new, j_new] = ;//visited
grid[i_new][j_new] = ;//go bad
rottinglist.Add(new int[] { i_new, j_new });
} //left:j-1>=0
i_new = i;
j_new = j - ;
if (j_new >= && TagGrid[i_new, j_new] == && grid[i_new][j_new] == )
{
FreshOrangeCount--;
RottenOrangeCount++;
TagGrid[i_new, j_new] = ;//visited
grid[i_new][j_new] = ;//go bad
rottinglist.Add(new int[] { i_new, j_new });
} //right:j+1<=column-1
i_new = i;
j_new = j + ;
if (j_new <= column - && TagGrid[i_new, j_new] == && grid[i_new][j_new] == )
{
FreshOrangeCount--;
RottenOrangeCount++;
TagGrid[i_new, j_new] = ;//visited
grid[i_new][j_new] = ;//go bad
rottinglist.Add(new int[] { i_new, j_new });
} }
if (rottinglist.Any())
{
Minute++;
}
foreach (var l in rottinglist)
{
Q.Enqueue(l);
}
rottinglist.Clear();
}
} public int OrangesRotting(int[][] grid)
{
row = grid.Length;
column = grid[].Length;
TagGrid = new int[row, column]; for (var i = ; i < row; i++)
{
for (var j = ; j < column; j++)
{
TagGrid[i, j] = ;//unvisited
if (grid[i][j] == )
{
FreshOrangeCount++;
}
if (grid[i][j] == )
{
RottenOrangeCount++;
Q.Enqueue(new int[] { i, j });
}
}
} BFS(grid); if (FreshOrangeCount != )
{
return -;
}
else
{
return Minute;
}
}
}
leetcode994的更多相关文章
- [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges
In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...
随机推荐
- 机器学习-Python中训练模型的保存和再使用
模型保存 BP:model.save(save_dir) SVM: from sklearn.externals import joblib joblib.dump(clf, save_dir) 模型 ...
- Mongo查询分组
db.test.aggregate( {'$match':{"url":/http:\/\/www.baidu.cn\/member\/T107581\//}}, {'$group ...
- MyBatis 多表查询
1.多表设计 多表查询 a.多表设计 (1)一对一 在任意一方设计外键保存另一张表的主键,维系表和表的关系 (2)一对多 在多的一方设计外键保存一的一方的主键,维系表和表的关系 (3)多对多 设计一张 ...
- 清除OpenStack网络与路由
下面,我们以删除一个路由设备为例,讲解整个过程(以下命令都在控制节点执行) 列出所有路由设备,获得要删除的路由器id # neutron router-list [root@controller ~] ...
- Navicat 12 破解方法
1.按步骤安装Navicat Premium,如果没有可以去官网下载:http://www.navicat.com.cn/download/navicat-premium 2.安装好后下载激活文件:h ...
- tomcat Error:NB:JAVA_HOME should point to a JDK not a JRE 解决方法
环境:win7 tomcata7.0解压版本 执行:service.bat install 报错:JAVA_HOME should point to a JDK not a JRE 网上找了几种解决方 ...
- C#存储过程中return参数
//1 连接字符串 string connectionString = "server=127.0.0.1;integrated security=true;database=MSPetSh ...
- java 获取两个日期之间的所有天数
Calendar start = Calendar.getInstance(); start.set(2017, 2, 20); Long startTIme = start.getTimeInMil ...
- C#多线程学习笔记之(abort与join配合使用)
今天刚开始学多线程,尽管以前用过一点点,但是只是照着网上代码抄,没有真正理解,现在回过头来想研究研究,慢慢弄懂,下面就是我在网上所找资料的例子,相信大家都看过,做了小点改动 View Code nam ...
- (转)C#读写共享文件
原文地址:https://www.cnblogs.com/guohu/p/4694515.html using System; using System.Collections.Generic; us ...