SPOJ拒绝BYTESM2的解决方案(魔法石)
SPOJ rejects solution to BYTESM2 (Philosopher's stone)
我在 SPOJ 上的页面上为BYTESM2问题编写了此解决方案。
法官不断为我的代码抛出WA(错误答案),我似乎无法弄清楚出了什么问题。
问题注释建议将cout
与printf
交换,但这不会改变任何事情。
#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 2 2
0 0
1 1
这种情况的答案应该是 1,而代码返回 0。
相关文章:
- 运行同一解决方案的另一个项目的项目
- Project Euler问题4的错误解决方案
- 计算每个节点的树高,帮助我解释这个代码解决方案
- C++:Application.cpp中抛出了未解析的外部符号(解决方案在问题的末尾,供未来的读者参考)
- visual c++,如何获取解决方案目录中的代码
- 有没有办法在远程设备上打开和编辑visual Studio 2017解决方案
- C++Matching Brackets 2解决方案不起作用
- 在 ubuntu3 上C++ goto 定义有什么解决方案吗16.04?
- 在 leetcode 上提交解决方案时出现堆栈缓冲区溢出错误
- 我的固定时间步长与增量时间和插值的解决方案是错误的吗?
- 无法在问题解决方案中执行输出逻辑
- 最大的回文产品 - 程序未运行,编写解决方案但无法理解问题
- 从预序遍历构造 bst 的 c++ 和 python 解决方案之间的区别
- 在一个解决方案中针对第三方静态库 (Creo) 的不同版本(版本)进行构建
- 如何巧妙地编写两个函数——一个用于检查是否存在解决方案,另一个用于获取所有解决方案
- 使用 Git 处理 C++ Visual Studio 2019 解决方案的外部依赖项源代码管理的最佳方法是什么?
- N-queen问题:无法弄清楚为什么我的解决方案不起作用
- 无法生成或清理 Visual Studio 2015 解决方案(访问被拒绝)
- SPOJ拒绝BYTESM2的解决方案(魔法石)
- Codechef拒绝我的解决方案