烂橘子

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值