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 ...
随机推荐
- 【java】之正则表达式摘要
构造 匹配 字符 x 字符 x \\ 反斜线字符 \0n 带有八进制值 0 的字符 n (0 <= n <= 7) \0nn 带有八进制值 0 的字符 nn (0 <= n < ...
- 力奋github:https://github.com/birdstudiocn
我的github地址https://github.com/birdstudiocn
- offse家族属性
在JavaScript中,常用offset.scroll和client家族属性来表示元素的位置和大小相关属性,最近在网上找到了一张图来表示三者之间的关系,正好可以在此借鉴一下. 本次主要来看一下off ...
- namenode namespaceID与datanode namespaceID 不一致导致datanode无法启动的问题
在启动hadoop之后,发现如下 datanode何在????? 在重新启动hadoop的时候,发现了问题,如下 这说明datanode启动时出错了 我们跟踪这个问题,进入到log文件夹下,找到had ...
- centos7安装Jenkins及其卸载
首先安装好Java(Java_home) 查看Java版本 # java -version 如果没安装,依照以下我的另一篇博客进行安装 https://www.cnblogs.com/djlsunsh ...
- [UE4]传值与传引用
值传递是圆形图标 设置引用需要使用Set by ref函数 对象在蓝图中都是以引用传递 对象,不需要额外设置参数类型是传值还是传引用. 结构体在蓝图中默认是按值传递 也可以手动设置结构体参数为按引用类 ...
- [UE4]AWP组合
AWP狙击枪可以由主枪和镜头模型组合而成. 一.主枪 二.镜头组合
- [UE4]场景加载界面
就可以这样就可以了,当另外一个场景成功打开后,场景加载界面也会自动消失(因为这是加载界面是添加到当前场景). 加上delay是为了在小场景测试的时候可以方便看到加载场景,避免场景加载过快看不到,不加的 ...
- [UE4]运行时进入观察模式
- Iptabels防火墙和SElinux
两者的区别: iptables用于设置防火墙(firewall), 即管理内外通信. iptables是Linux下功能强大的应用层防火墙工具iptables 能做到“控制内部机器上网与不上网,访问哪 ...