日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

烂橘子

發布時間:2023/12/1 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 烂橘子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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:23 52 1 0 2 1 1 0 1 2 1 1 0 0 2 1Output:2

Explanation:

說明:

Algorithm:

算法:

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

為了解決這個問題,我們使用BFS和隊列 數據結構 。

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

    首先,我們將所有位置推入具有2的隊列中,并通過將NULL插入隊列來進行分區。

  • 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值插入到隊列中,我們從隊列中彈出的每一個元素。

  • 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時,并且如果它不是隊列的最后一個元素,那么我們都會增加計數值。

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

    重復步驟2到3,直到隊列為空。

  • .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}} .minHeight{min-height: 250px;}@media (min-width: 1025px){.minHeight{min-height: 90px;}}

    爛橙問題的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;elsecout<<store<<endl;}return 0; }

    Output

    輸出量

    Number of days are : 2

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

    創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

    總結

    以上是生活随笔為你收集整理的烂橘子的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。