我在Google Jam 2020竞赛中几乎没有存档问题。 首先,我在资格赛中遇到了5个问题,一切正常。唯一的问题是我的算法太慢了,所以我得不到最高分。 但是没问题。我进入第1轮回合,有3个问题。问题3称为“广场舞”。 它的描述可以在这里找到: https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd74/00000000002b1355
我做了一个程序。我的程序成功通过了示例案例。 我还测试了我的程序在其他几种情况下的正常运行。 但是,当我上传解决方案时,我在Google测试用例集中得到了“错误答案”。
例如。这种情况最适合我的解决方案:
1
5 5
1 1 1 1 1
1 1 1 1 1
1 1 2 1 1
1 1 1 1 1
1 1 1 1 1
它给出了答案66,因为它将进行3次回合:
1 1 1 1 1
1 1 1 1 1
1 1 2 1 1
1 1 1 1 1
1 1 1 1 1
26
1 1 1 1 1
1 1 0 1 1
1 0 2 0 1
1 1 0 1 1
1 1 1 1 1
22
1 1 0 1 1
1 1 0 1 1
0 0 2 0 0
1 1 0 1 1
1 1 0 1 1
18
所以一切正常。
但是为什么它在Google测试中失败。
有什么我不理解的东西吗? 还是一些代码错误?但这对我测试过的许多测试用例都行得通...
我的源代码:
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
int sizeX;
int sizeY;
int** map;
bool** eliminateTable;
bool calculateWhoToEliminate()
{
for (int y = 0; y < sizeY; ++y)
{
memset(eliminateTable[y], 0, sizeX);
}
bool toEliminate = false;
for (int y = 0; y < sizeY; ++y)
{
for (int x = 0; x < sizeX; ++x)
{
if (map[y][x] == 0)
continue;
int skillOfNeightbours = 0;
int neightboursCount = 0;
//w lewo
int px = x - 1;
while (px >= 0)
{
if (map[y][px] != 0)
{
skillOfNeightbours += map[y][px];
neightboursCount++;
break;
}
px--;
}
//w prawo
px = x + 1;
while (px < sizeX)
{
if (map[y][px] != 0)
{
skillOfNeightbours += map[y][px];
neightboursCount++;
break;
}
px++;
}
//w gore
int py = y - 1;
while (py >= 0)
{
if (map[py][x] != 0)
{
skillOfNeightbours += map[py][x];
neightboursCount++;
break;
}
py--;
}
//w dol
py = y + 1;
while (py < sizeY)
{
if (map[py][x] != 0)
{
skillOfNeightbours += map[py][x];
neightboursCount++;
break;
}
py++;
}
if (map[y][x] * neightboursCount < skillOfNeightbours)
{
eliminateTable[y][x] = true;
toEliminate = true;
}
}
}
return toEliminate;
}
void eliminate()
{
for (int y = 0; y < sizeY; ++y)
{
for (int x = 0; x < sizeX; ++x)
{
if (eliminateTable[y][x])
{
map[y][x] = 0;
}
}
}
}
int countScore()
{
int score = 0;
for (int y = 0; y < sizeY; ++y)
{
for (int x = 0; x < sizeX; ++x)
{
score += map[y][x];
}
}
return score;
}
int solve()
{
int score = 0;
while (true)
{
score += countScore();
if (calculateWhoToEliminate())
{
eliminate();
}
else
{
break;
}
}
return score;
}
int main()
{
int testCasesCount;
cin >> testCasesCount;
for (int test = 1; test <= testCasesCount; ++test)
{
cin >> sizeX;
cin >> sizeY;
map = new int*[sizeY];
eliminateTable = new bool*[sizeY];
for (int y = 0; y < sizeY; ++y)
{
map[y] = new int[sizeX];
eliminateTable[y] = new bool[sizeX];
for (int x = 0; x < sizeX; ++x)
{
cin >> map[y][x];
}
}
int r = solve();
cout << "Case #" << test << ": " << r << endl;
for (int y = 0; y < sizeY; ++y)
{
delete map[y];
delete eliminateTable[y];
}
delete map;
delete eliminateTable;
}
//system("pause");
return 0;
}
答案 0 :(得分:0)
这是否能解决问题,绝对是一个错误:
memset(eliminateTable[y], 0, sizeX);
这应该是:
memset(eliminateTable[y], 0, sizeX * sizeof(bool));
memset
函数的第三个参数是字节数,而不是项目数。
可能您正在执行的memset
调用仅部分填充了bool
数组中的0,因此由于使用了bool
数组的未初始化部分,因此无法获得正确的结果。
答案 1 :(得分:0)
好。我找到了解决方法。
得分必须很长
不是: cin >> sizeX; cin >> sizeY; 但: cin >> sizeY; cin >> sizeX;
因为首先是行,然后是列。