【问题标题】:Recursion with matrices - Rotten Oranges矩阵递归 - 烂橙子
【发布时间】:2015-10-23 12:17:48
【问题描述】:

我正在尝试解决以下问题:

给定一个维度矩阵n*n,其中矩阵中的每个单元格都可以有值012,含义如下:

0:empty cell
1:cells have fresh oranges
2:cells have rotten oranges

确定所有橙子腐烂所需的最短时间。索引为[i,j] 的腐烂橙子可以腐烂索引为[i+1,j][i,j+1][i-1,j][i,j-1] 的其他新鲜橙子。如果不可能把每一个橙子都腐烂,那么只需返回 -1;

以下是我尝试的解决方案

#include<bits/stdc++.h>
using namespace std;
void recur(int a[100][100],int t[100][100],int count,int i,int j,int n)
{
    cout<<i<<" "<<j<<endl;
    if(i<0||i>=n||j<0||j>=n)//if out of boundary stop
    {
    //  cout<<"1\n";
        //return;
    }
    if(a[i][j]==1)  //if fresh orange then recur from here too
    {
    //  cout<<"2\n";
        t[i][j]=min(count,t[i][j]);//take this case 2 1 1 2...Third 1 can be rot in 2seconds as well as 1 second so I sotre minimum of both

        recur(a,t,count+1,i+1,j,n);//again recur in 4 directions
        recur(a,t,count+1,i,j+1,n);
        recur(a,t,count+1,i,j-1,n);
        recur(a,t,count+1,i-1,j,n);

    }
    else if(a[i][j]==2)//if already a rot orange no need to check in 4 directions as I am checking for every rot in loop in main()
    {
    /// cout<<"3\n";
        t[i][j]=0;//already rot so time required =0 seconds
    //  return;
    }
    else if(a[i][j]==0)//if empty space stop
    {
    //  cout<<"4\n";
    //  return;
        //cout<<"hi\n";
    }

}
main()
{
    int a[100][100],t[100][100],n;//a is input array and t is array whihc stores time required to rot each fresh orange
    cin>>n;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            cin>>a[i][j];
            t[i][j]=INT_MAX;
        }
    }
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            if(a[i][j]==2)
            {
                recur(a,t,1,i+1,j,n);// recursion starts for top bottom left and right
                recur(a,t,1,i,j+1,n);
                recur(a,t,1,i,j-1,n);
                recur(a,t,1,i-1,j,n);

            }
        }
    }
    int flag=0,val=0;
    for(int i=0;i<n;++i)
    {
        for(int j=0;j<n;++j)
        {
            if(a[i][j]==1&&t[i][j]==INT_MAX)
            {   
                flag=-1;
                break;
            }   
            else if(a[i][j]==1)
            val=max(t[i][j],val);
        }
    }
    if(flag==-1)
    val=-1;
/*  else
    {

        for(int i=0;i<n;++i)
        {
            for(int j=0;j<n;++j)
            cout<<t[i][j]<<" ";
            cout<<endl;
        }
    }*/
    cout<<val;
}

但是,这种情况会无限期地重复。为什么?

【问题讨论】:

  • 由于tcount 不会影响控制流程,您不妨将它们也删除,同时简化函数以跟踪错误。
  • t 和 count 很重要...请参阅我正在递归发送 count+1 并且 t 存储计数值..
  • 这是作业问题还是项目?有点难以理解你在这里问什么。我建议解释一下背景和您当前的方法,然后包括一个更短的代码示例。
  • 它实际上是我正在尝试的一些面试问题
  • 太长,太多重复,太多返回被注释掉,太多单字母变量名,复杂的多维数组索引,没有明显的调试尝试。

标签: c++ algorithm recursion data-structures


【解决方案1】:

请在表格中问一个问题,为什么这个程序在无限循环中运行或类似的东西。

看看你的递归

 recur(a,t,count+1,i+1,j,n); // 1
 recur(a,t,count+1,i,j+1,n); // 2
 recur(a,t,count+1,i,j-1,n); // 3
 recur(a,t,count+1,i-1,j,n); // 4

路径 (1)->(4)->(1)->(4) 是无限的,因为你增加 i 然后减少 i。

【讨论】:

    猜你喜欢
    • 2015-07-07
    • 2018-03-29
    • 1970-01-01
    • 2017-10-15
    • 2019-11-13
    • 1970-01-01
    • 1970-01-01
    • 2018-03-14
    • 2018-11-27
    相关资源
    最近更新 更多