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的更多相关文章

  1. [Swift]LeetCode994. 腐烂的橘子 | Rotting Oranges

    In a given grid, each cell can have one of three values: the value 0 representing an empty cell; the ...

随机推荐

  1. 天朝屁民每天做T跟菜贩一样,进菜-卖菜,为伟大的菜贩精神点赞

    天朝屁民每天做T跟菜贩一样,进菜-卖菜,为伟大的菜贩精神点赞

  2. Importing multi-valued field into Solr from mySQL using Solr Data Import Handler

    http://stackoverflow.com/questions/20233837/importing-multi-valued-field-into-solr-from-mysql-using- ...

  3. 学习笔记之1001 Inventions That Changed the World

    1001 Inventions That Changed the World: Jack Challoner: 9780764161360: Amazon.com: Books https://www ...

  4. LeetCode 搜索二维矩阵 II

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  5. go语言学习--map的并发

    go提供了一种叫map的数据结构,可以翻译成映射,对应于其他语言的字典.哈希表.借助map,可以定义一个键和值,然后可以从map中获取.设置和删除这个值,尤其适合数据查找的场景.但是map的使用有一定 ...

  6. ROS设备的性价比图

  7. JavaScript中的类(class)、构造函数(constructor)、原型(prototype)

    类 Class 类的概念应该是面向对象语言的一个特色,但是JavaScript并不像Java,C++等高级语言那样拥有正式的类,而是多数通过构造器以及原型方式来仿造实现.在讨论构造器和原型方法前,我可 ...

  8. python面向对象 : 反射和内置方法

    一. 反射 1. isinstance()和issubclass() isinstance( 对象名, 类名) : 判断对象所属关系,包括父类  (注:type(对象名) is 类名 : 判断对象所属 ...

  9. [UE4]Delay与Retriggerable Delay

    一.Delay 在右上角都有一个表盘的图标,表示不是在当帧内执行,而是需要一定时间才能完成的. 鼠标移上去,会有一段文字注释.根据指定的延迟时间执行一个延后的动作.当计时还没有结束的时候,如果再次被调 ...

  10. F5负载均衡原理(转载)

    https://blog.csdn.net/panxueji/article/details/42647193 一. 负载均衡技术 负载均衡技术在现有网络结构之上提供了一种廉价.有效.透明的方法,来扩 ...