我写了一个程序来用 C++ 解决 uva 的扫雷问题(10189)。
我已经编写了代码,它似乎在我的机器上正常工作。不幸的是,我的提交似乎给了我运行时错误(在这种情况下,uva 没有提及错误消息或原因)。我在这里粘贴代码:
using namespace std;
void nullCheck(void *ptr)
{
exit(0);
}
typedef unsigned int uint;
class Field
{
public:
vector <vector<uint> > nghBombCnt; //neighbourBombCount
vector <vector<bool> > isBomb;
uint width;
uint height;
public:
Field(uint w, uint h)
{
}
Field() { }
~Field()
{
}
};
int main(void)
{
vector<Field> fieldList;
do
{
Field curField;
uint width, height;
scanf("%u %u", &height, &width);
curField.width = width;
curField.height = height;
for (uint rowCtr = 0; rowCtr < curField.height; rowCtr++)
{
vector<bool> curBombRow;
for (uint colCtr = 0; colCtr < curField.width; colCtr++)
{
char curSymb;
scanf("%1s", &curSymb);
switch (curSymb)
{
case '.':
curBombRow.push_back(false); break;
case '*':
curBombRow.push_back(true); break;
default: break;
}
}
curField.isBomb.push_back(curBombRow);
}
if (!(curField.width == 0 && curField.height == 0))
fieldList.push_back(curField);
else
break;
} while (1);
for (uint fieldCtr = 0; fieldCtr < fieldList.size(); fieldCtr++)
{
Field curField = fieldList.at(fieldCtr);
for (uint rowCtr = 0; rowCtr < curField.height; rowCtr++)
{
vector<uint> curRow;
for (uint colCtr = 0; colCtr < curField.width; colCtr++)
{
uint bombCnt = 0;
if (curField.isBomb[rowCtr][colCtr])
{
curRow.push_back(0);
continue;
}
if (colCtr != 0 && curField.isBomb[rowCtr][colCtr - 1])
bombCnt++; //east
if (rowCtr != 0 && curField.isBomb[rowCtr - 1][colCtr])
bombCnt++; //north
if (rowCtr != curField.height - 1
&& curField.isBomb[rowCtr + 1][colCtr])
bombCnt++; //south
if (colCtr != curField.width - 1
&& curField.isBomb[rowCtr][colCtr + 1])
bombCnt++; //west
if (colCtr != curField.width - 1 && rowCtr != 0
&& curField.isBomb[rowCtr -1][colCtr + 1])
bombCnt++; //north-east
if (colCtr != 0 && rowCtr != curField.height - 1
&& curField.isBomb[rowCtr + 1][colCtr - 1])
bombCnt++; // south-west
if (colCtr != curField.width - 1
&& rowCtr != curField.height - 1
&& curField.isBomb[rowCtr + 1][colCtr + 1])
bombCnt++; //south-east
if (colCtr != 0 && rowCtr != 0
&& curField.isBomb[rowCtr - 1][colCtr - 1])
bombCnt++; //north-west
curRow.push_back(bombCnt);
}
curField.nghBombCnt.push_back(curRow);
}
fieldList[fieldCtr] = curField;
}
for (uint fieldNo = 0; fieldNo < fieldList.size(); fieldNo++)
{
printf("Field #%u:\n", fieldNo + 1);
Field curField = fieldList.at(fieldNo);
for (uint rowCtr = 0; rowCtr < curField.height; rowCtr++)
{
for (uint colCtr = 0; colCtr < curField.width; colCtr++)
{
if (curField.isBomb[rowCtr][colCtr])
printf("*");
else
printf("%u", curField.nghBombCnt[rowCtr][colCtr]);
}
printf("\n");
}
printf("\n");
}
return 0;
}