烂橘子

Problem Statement:

问题陈述:

Given a matrix of dimension r*c where each cell in the matrix can have values 0, 1 or 2 which has the following meaning:

给定尺寸r * C的矩阵,其中矩阵中的每个单元可以具有其具有以下含义的值0,1或2:

  • 0 : Empty cell

    0:空单元格

  • 1 : Cells have fresh oranges

    1:细胞有新鲜的橘子

  • 2 : Cells have rotten oranges

    2:细胞有烂橘子

So, we have to determine what is the minimum time required to all oranges. A rotten orange at index [i,j] can rot other fresh orange at indexes [i-1,j], [i+1,j], [i,j-1], [i,j+1] (up, down, left and right) in unit time. If it is impossible to rot every orange then simply return -1.

因此,我们必须确定所有橙子所需的最短时间是多少。 索引为[i,j]的烂橙可以使索引为[i-1,j] , [i + 1,j] , [i,j-1] , [i,j + 1]的其他鲜橙腐烂(向上,向下,向左和向右)。 如果不可能腐烂每个橙色,则只需返回-1即可 。

Example:

例:

    Input:
2
3 5
2 1 0 2 1 1 0 1 2 1 1 0 0 2 1
Output:
2

Explanation:

说明:

rotten oranges

Algorithm:

算法:

To implement this question we use BFS and a queue data structure.

为了解决这个问题,我们使用BFS和队列 数据结构 。

  1. At first, we push all positions into the queue which has 2 and make a partition by inserting NULL into the queue.

    首先,我们将所有位置推入具有2的队列中,并通过将NULL插入队列来进行分区。

  2. We pop every element from the queue until the first NULL comes and Go for its four neighbor's if there is any 1 then make it two and push it into the queue and separate this section again inserting a NULL into the queue.

    直到第一个NULL来,去它的四个邻居的,如果有任何1然后将其扩大两倍,并将它推到队列中,分离这部分再插入一个NULL值插入到队列中,我们从队列中弹出的每一个元素。

  3. Whenever we encounter NULL except for the first NULL and if it is not a last element of the queue then we increase the count value.

    每当我们遇到除第一个NULL之外的NULL时,并且如果它不是队列的最后一个元素,那么我们都会增加计数值。

  4. Repeat step 2 to 3 until the queue is empty.

    重复步骤2到3,直到队列为空。

烂橙问题的C ++实现 (C++ Implementation for Rotten Oranges problem)

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool zero(int *arr,int r,int c){
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(*((arr+i*c)+j)==1)
return false;
}
}
return true;
} 
int rotten(int *arr,int r,int c){
queue<pair<int,int> >q;
int store=0,temp=0;
for(int i=0;i<r;i++){
for(int j=0;j<c;j++){
if(*((arr+i*c)+j)==2){
q.push(make_pair(i,j));
}
}
}
q.push(make_pair(-1,-1));
while(!q.empty()){
pair<int,int> p=q.front();
q.pop();
if(p.first!=-1){
if(*((arr+p.first*c)+p.second)==1){
temp=1;
*((arr+p.first*c)+p.second)=2;
}
if(p.first==0 && p.second==0){
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
}
else if(p.first==0 && p.second==c-1){
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
}
else if(p.first==0){
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
}
else if(p.first==r-1 && p.second==0){
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
}
else if(p.second==0){
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
}
else if(p.first==r-1 && p.second==c-1){
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
}
else if(p.first==r-1){
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
}
else if(p.second==c-1){
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
}
else{
if(*((arr+(p.first)*c)+p.second-1)==1){
q.push(make_pair((p.first),p.second-1));
}
if(*((arr+(p.first)*c)+p.second+1)==1){
q.push(make_pair((p.first),p.second+1));
}
if(*((arr+(p.first-1)*c)+p.second)==1){
q.push(make_pair((p.first-1),p.second));
}
if(*((arr+(p.first+1)*c)+p.second)==1){
q.push(make_pair((p.first+1),p.second));
}
}
}
if(p.first==-1){
if(!q.empty()){
q.push(make_pair(-1,-1));
}
if(temp==1){
store++;
temp=0;
}
}
}
return store;
}
int main() {
int num;
cin>>num;
for(int i=0;i<num;i++){
int r,c;
cin>>r>>c;
int arr[r][c];
for(int j=0;j<r;j++){
for(int k=0;k<c;k++){
cin>>arr[j][k];
}
}
int store=rotten(&arr[0][0],r,c);
if(!zero(&arr[0][0],r,c))
cout<<"-1"<<endl;
else
cout<<store<<endl;
}
return 0;
}

Output

输出量

Number of days are : 2

翻译自: https://www.includehelp.com/icp/rotten-oranges.aspx

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/377033.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

android junit 测试程序

http://blog.csdn.net/to_cm/article/details/5704783 Assert.assertEquals(2, t); 断言转载于:https://www.cnblogs.com/wjw334/p/3714120.html

MySQL 8.0.22执行器源码分析HashJoin —— BuildHashTable函数细节步骤

BuildHashTable函数细节步骤 该函数位置处于hash_join_iterator.cc 403 ~ 560行 step1&#xff1a;如果被驱动表迭代器没有更多的行数&#xff0c;更新m_state为EOR&#xff0c;然后返回false&#xff0c;表明创建hash表失败 if (!m_build_iterator_has_more_rows) {m_state…

《那些年啊,那些事——一个程序员的奋斗史》——125

距离离职交接的一个月时间还剩几天&#xff0c;本来应该是平淡无事的&#xff0c;却没想到最后还是波澜四起。昨天下班前&#xff0c;公司突然停了电。这本是件普通得不能再普通的事情&#xff0c;可没想到过了一会来电了&#xff0c;或许是波峰电压太大&#xff0c;或许是稳压…

python中的元类_Python中的元类

python中的元类Python元类 (Python metaclass) A metaclass is the class of a class. A class defines how an instance of a class i.e.; an object behaves whilst a metaclass defines how a class behaves. A class is an instance of a metaclass. 元类是类的类。 一个类…

MySQL 8.0.22执行器源码分析HashJoin —— 一些初始化函数的细节步骤

目录InitRowBuffer&#xff08;101行~126行&#xff09;InitProbeIterator&#xff08;142行~153行&#xff09;*HashJoinIterator* 的Init&#xff08;155行~240行&#xff09;InitializeChunkFiles&#xff08;364行~401行&#xff09;InitWritingToProbeRowSavingFile&#…

c语言的宏定义学习笔记

宏定义 在预处理之前&#xff0c;c预处理器会对代码进行翻译&#xff0c;譬如用blank替换注释&#xff0c;去掉多余的空格&#xff0c;删除末尾的\来拼接行等。 例如&#xff1a; int /*注释*/ x; 会被翻译成 int x; printf("this is a s\ entence."); 会被翻译成 pr…

摄氏温度转换华氏温度_什么是摄氏温度?

摄氏温度转换华氏温度摄氏温度 (Celsius) Celsius is a temperature measuring scale which as a SI unit derived from the seven base units stated and described by the International System of Units (SI). 摄氏温度是一种温度测量刻度&#xff0c;它是由国际单位制(SI)所…

别人的算法学习之路

http://www.cnblogs.com/figure9/p/3708351.html 我的算法学习之路 关于 严格来说&#xff0c;本文题目应该是我的数据结构和算法学习之路&#xff0c;但这个写法实在太绕口——况且CS中的算法往往暗指数据结构和算法&#xff08;例如算法导论指的实际上是数据结构和算法导论&a…

git config命令使用第二篇——section操作,多个key值操作,使用正则

接上一篇&#xff0c;git config命令使用第一篇——介绍&#xff0c;基本操作&#xff0c;增删改查:http://blog.csdn.net/hutaoer06051/article/details/8275069 1. 删除一个section 命令参数 --remove-section 格式&#xff1a;git config [--local|--global|--system] --rem…

MySQL面试准备——64页pdf

本笔记为以前整理的零碎的关于Mysql的知识点&#xff0c;有深入源码的也有浅层的八股。已经被我整理成了一个pdf。 实习岗位正好也是和数据库内核有关的&#xff0c;之后应该还会更新。做个整理&#xff0c;方便秋招的时候快速回顾吧。 链接&#xff1a;链接 提取码&#xff1a…

python点图_Python | 点图

python点图The dot plot is a type of data representation in which each data-point in the figure is represented as a dot. Dot plot underlies discrete functions unlike a continuous function in a line plot. Each value could be correlated but cannot be connecte…

SAP-MM:发票、贷方凭证、事后借记、后续贷记

发票和事后借记 相同点&#xff1a;增加对供应商的应付款 不同点&#xff1a;针对同一订单收货&#xff0c;发票要先于事后借记&#xff08;事后借记是对供应商后期发票金额的补充&#xff09;&#xff1b;发票和金额、订单数量有关系&#xff0c;而事后借记只是订单金额调整的…

Dijkstra for MapReduce (1)

<math xmlns"http://www.w3.org/1998/Math/MathML"><mi>x</mi><mo>,</mo><mi>y</mi><mo>&#x2208;<!-- ∈ --></mo><mi>X</mi> </math> 准备研究一下Dijkstra最短路径算法Hadoop上…

sql的外键约束和主键约束_SQL约束

sql的外键约束和主键约束SQL | 约束条件 (SQL | Constraints) Constraints are the guidelines implemented on the information sections of a table. These are utilized to restrict the kind of information that can go into a table. This guarantees the precision and …

nios pio interrupt 的使能

关于nios 中的中断&#xff0c;因为要16c550中需要nios的中断环境去测试&#xff0c;所以就用到了中断。 硬件&#xff1a;在nios中添加硬件PIO,但是要使能中断功能。如下图所示&#xff1a; 系统列化&#xff0c;PIO的连接就不说了。但是要注意两地方&#xff1a;edge type&am…

《单线程的build hash table、write rows to chunks、hash join的步骤以及流程图》

Build Hash Table流程 1、初始化row buffer2、从build input table中读一行3、若读完build input table所有row&#xff0c;返回状态READING_ROW_FROM_PROBE_item4、否则&#xff0c;向hash map中写入一条row5、如果hash map 写入成功&#xff0c;返回2&#xff0c;继续执行6、…

在Scala的溪流

Scala | 流 (Scala | Streams) Stream in Scala is a type of lazy val. It is a lazy val whose elements are evaluated only when they are used in the program. Lazy initialization is a feature of Scala that increases the performance of the program. Scala中的Stre…

适合高速驱动电路的推挽电路

http://www.dzsc.com/data/html/2008-9-10/69023.html 图1是使用NPN/PNP型晶体管的互补推挽电路&#xff0c;适于驱动功率MOSFET的门极。此电路虽然具有门极电流的驱动能力&#xff0c;但射极输出波形不能比输人信号快。 图2是此电路的开关波形。它表示出tf、tr都快&#xff0c…

cholesky分解

接着LU分解继续往下&#xff0c;就会发展出很多相关但是并不完全一样的矩阵分解&#xff0c;最后对于对称正定矩阵&#xff0c;我们则可以给出非常有用的cholesky分解。这些分解的来源就在于矩阵本身存在的特殊的 结构。对于矩阵A&#xff0c;如果没有任何的特殊结构&#xff0…

socket编程常见函数使用方法

socket知识 有了IP地址&#xff0c;socket可知道是与哪一台主机的哪一个进程通信 有了端口号&#xff0c;就知道是这个进程的哪一个套接字进行传输 应用进程使用描述符与它的套接字进行通信&#xff0c;也就是说一个进程创建一个套接字时就会返回一个套接字描述符 socket的…