SPOJ拒绝解决BYTESM2(哲学家的石头)

时间:2016-01-26 16:20:12

标签: c++ algorithm dynamic-programming

我在Page on SPOJ上为BYTESM2问题编写了这个解决方案 法官经常为我的代码抛出WA(错误的答案),我似乎无法弄清楚是什么错误。
问题评论建议将coutprintf进行交换,但这不会改变任何内容。

#include <iostream>

using namespace std;

int Memo[100][100];

void RefreshMemo()
{
    for (int i = 0; i < 100; ++i)
        for (int j = 0; j < 100; ++j)
            Memo[i][j] = -1;
}

inline int Max(int a, int b, int c = -1)
{
    if( a > b && a > c)
        return a;
    else if( b > a && b > c)
        return b;
    return c;
}

int CollectMax(int Stones[100][100], int i, int j, int MaxRow, int MaxColumn)
{
    if (Memo[i][j] > 0)
        return Memo[i][j];

    else
    {
        if (i == MaxRow)
            Memo[i][j] = Stones[i][j];
        else if (j == 0)
            Memo[i][j] = Stones[i][j] + Max( CollectMax(Stones, i+1, j, MaxRow, MaxColumn), CollectMax(Stones, i+1, j+1, MaxRow, MaxColumn) );
        else if (j == MaxColumn)
            Memo[i][j] = Stones[i][j] + Max( CollectMax(Stones, i+1, j, MaxRow, MaxColumn), CollectMax(Stones, i+1, j-1, MaxRow, MaxColumn) );  
        else
            Memo[i][j] = Stones[i][j] + Max( CollectMax(Stones, i+1, j, MaxRow, MaxColumn), CollectMax(Stones, i+1, j+1, MaxRow, MaxColumn), CollectMax(Stones, i+1, j-1, MaxRow, MaxColumn) );

        return Memo[i][j];
    }
}

int main()
{
    int t;
    cin>>t;
    while ( t-- && t >= 0)
    {
        int Stones[100][100], h, w;
        int Answer = 0;
        cin>>h>>w;

        for (int i = 0; i < h; ++i)
            for (int j = 0; j < w; ++j)
                cin>>Stones[i][j];
        RefreshMemo();  
        for (int i = 0; i < w; ++i)
        {
            int TryI = CollectMax(Stones, 0, i, h-1, w-1);
            if(Answer < TryI)
                Answer = TryI;
        }
        cout<<Answer<<endl;
    }
    return 0;
}

1 个答案:

答案 0 :(得分:1)

当某些细胞不含任何结石时,您的解决方案无法处理。例如,考虑输入:

  

1
  2 2
  0 0
  1 1

这种情况的答案应该是1,而你的代码返回0。