爛橘子

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.pswp.cn/news/377033.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/377033.shtml
英文地址,請注明出處:http://en.pswp.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的…