我的填充算法幾乎完成,但有一個小錯誤,我花了大約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