我的填充算法几乎完成,但有一个小错误,我花了大约3个小时的时间调试,但我似乎无法找到它!C++ Floodfill算法最终错误
注: 当我使用数字从0至15,以限定壁读取
1 =顶部 2 =右 4 =底部 8 =左 (所以13将意味着该顶/下/左墙是有)
我的计划:
它读取字段数来计算(所以一切BEL空间最大这里的ow是一个重复的字段数量的循环)。
然后它获取房间的尺寸
在类字段现在,它创建的对象(细胞)的阵列,其存储所述壁围绕(左右向下向上),并且低于16
的值
现在,这里是我觉得问题就来了,通过阅读性病价值观:: CIN
,然后在读的一切,它扫描空(0),然后创建一个房间,并检查为它周围的可用空间(使用墙壁检查)
并在最后它返回最大值,我们完成了。
输入我用:
1
2 2
13 3
15 14
所以会发生什么是是,什么地方,或壁挂式检查,或者一个目标小区的东西创造出了问题(我认为)
这是我的脚本,很抱歉不得不问这样的傻事!
在此先感谢
// een simpele floodfill
#include <stdlib.h>
#include <iostream>
#include <bitset>
class Cell {
private:
int kamer, value;
bool left, right, up, down;
public:
// constructor
Cell::Cell() {};
// functions
bool CanLeft() { return left ; }
bool CanRight() { return right; }
bool CanDown() { return down ; }
bool CanUp() { return up ; }
int GetRoom() { return kamer; }
void SetRoom(int x) { kamer = x ; }
void SetValue(int x, int room=0) { value = x;
kamer = room;
std::bitset<sizeof(int)> bits(value);
if (bits[3]) left = true;
else left = false;
if (bits[2]) down = true;
else down = false;
if (bits[1]) right = true;
else right = false;
if (bits[0]) up = true;
else up = false;
}
};
class Field {
private:
int Biggest_Chamber;
int Y;
int X;
int temp;
Cell playfield[][1];
public:
// constructor
Field::Field(int SizeY, int SizeX) {
Y = SizeY;
X = SizeX;
Cell playfield[SizeY-1][SizeX-1];
}
// Create a 2d array and fill it
void Get_input() {
for (int Yas = 0; Yas < Y; Yas++){
for (int Xas = 0; Xas < X; Xas++){
std::cin >> temp;
playfield[Yas][Xas].SetValue(temp);
}
}
};
void Start() { Mark(0,0,1); }
void Mark(int y, int x, int nr) {
std::cout << nr;
temp = nr;
playfield[y][x].SetRoom(nr);
if (playfield[y][x].CanLeft()) {
if (playfield[y][x-1].GetRoom() != 0) {
Mark(y, x-1, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanDown()) {
if (playfield[y+1][x].GetRoom() != 0) {
Mark(y+1, x, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanRight()) {
if (playfield[y][x+1].GetRoom() != 0) {
Mark(y, x+1, nr);
std::cout << nr;
system("pause");}}
if (playfield[y][x].CanUp()) {
if (playfield[y-1][x].GetRoom() != 0) {
Mark(y-1, x, nr);
std::cout << nr;
system("pause");}}
for (int vertical = 0; vertical < Y; vertical++) {
for (int horizontal = 0; horizontal < X; horizontal++) {
if (playfield[vertical][horizontal].GetRoom() == 0) Mark(vertical, horizontal, nr+1);
}
}
}
int MaxValue() {
int counter[temp];
int max = 0;
for (int y = 0; y < Y; y++) {
for (int x = 0; x < X; x++) {
counter[playfield[y][x].GetRoom()]++;
}
}
for (int i = 0; i < temp; i++)
{
if (counter[i] > max)
max = counter[i];
}
return max;
}
};
int main() {
using namespace std;
int NrKamers;
int sizeY;
int sizeX;
std::cin >> NrKamers;
for (int i = 0; i < NrKamers; i++){
std::cin >> sizeY >> sizeX;
Field floodfield(sizeY, sizeX);
floodfield.Get_input();
floodfield.Start();
std::cout << floodfield.MaxValue() << std::endl;
}
return 0;
}
什么是错误讯息? 'class Field'已经有一个右大括号('};')。这是一个错字,你又在做同样的事情吗? – Mahesh 2011-05-09 07:56:23
分段错误 - 但通过调试,我发现以某种方式在Markeer(int,int,int)中遇到无限循环,什么?在它后面的';'? – Buster 2011-05-09 07:57:33
在发布之前将您的变量名称和评论翻译成英文会很有礼貌;并不是每个人都能理解荷兰语。 – 2011-05-09 08:09:58