请问这个代码为什么每次只能运行一两百行就停止了?就是我的的数据输出来最多只能输出stage1和两百行x y

#include

#include

#include

#include

using namespace std;

const int WIDTH = 1000;

const int HEIGHT = 1000;

static char s_Grid[WIDTH][HEIGHT];

static int s_YRange;

static int s_XRange;

static int s_YCenter;

static int s_XCenter;

static int s_XLow;

static int s_YLow;

static int s_XHigh;

static int s_YHigh;

void DLA_Init(void)

{

int x, y;

s_XCenter = WIDTH / 2;

s_YCenter = HEIGHT / 2;



for (y = 0; y < HEIGHT; y++)

{

    for (x = 0; x < WIDTH; x++)

    {

        s_Grid[y][x] = 0;

    }

}

s_Grid[s_YCenter][s_XCenter] = 1;

cout<< s_XCenter << "  " << s_YCenter << endl;

//cout << "#XCenter = " << s_XCenter << endl;

//cout << "#YCenter = " << s_YCenter << endl;

}

static inline void walk(int *pX, int *pY)

{

int w;

w = rand() % 8;

//cout << "w = " << w << endl;

switch (w)

{

case 0: // Right

    (*pX)++;

    break;

case 1: // Left

    (*pX)--;

    break;

case 2: // Down

    (*pY)++;

    break;

case 3: // Up

    (*pY)--;

    break;

case 4: // Right Down

    (*pX)++;

    (*pY)++;

    break;

case 5: // Right Up

    (*pX)++;

    (*pY)--;

    break;

case 6: // Left Down

    (*pX)--;

    (*pY)++;

    break;

case 7: // Left Up

    (*pX)--;

    (*pY)--;

    break;

default:

    break;

}

//cout << "X = " << *pX << " ,Y = " << *pY << endl;

if (*pX > s_XHigh) *pX = s_XLow;

if (*pY > s_YHigh) *pY = s_YLow;

if (*pX < s_XLow) *pX = s_XHigh;

if (*pY < s_YLow) *pY = s_YHigh;

//cout << "X = " << *pX << " ,Y = " << *pY << endl;

return;

}

static inline bool isAdjacent(int x, int y)

{

//***

//* *

//***

//临近位置包括8个点

int xx, yy;

xx = x + 1;

if (xx > s_XHigh) xx = s_XLow;

if (s_Grid[y][xx] == 1) return true;// Right



yy = y + 1;

if (yy > s_YHigh) yy = s_YLow;

if (s_Grid[yy][xx] == 1) return true; // Right Down



if (s_Grid[yy][x] == 1) return true; // Down



yy = y - 1;

if (yy < s_YLow) yy = s_YHigh;

if (s_Grid[yy][xx] == 1) return true; //Right Up



if (s_Grid[yy][x] == 1) return true; // Up



xx = x - 1;

if (xx < s_XLow) xx = s_XHigh;

if (s_Grid[yy][xx] == 1) return true; //Left Uo



if (s_Grid[y][xx] == 1) return true; // Left



yy = y + 1;

if (yy > s_YHigh) yy = s_YLow;

if (s_Grid[yy][xx] == 1) return true; // Left Down



return false;

}

void DLA_Gen(int *pX, int *pY)

{

//int i = 0;    do

{

    *pX = rand() % s_XRange + s_XLow;

    *pY = rand() % s_YRange + s_YLow;

}

while (s_Grid[*pY][*pX] == 1);

//*pX = s_XLow;

//*pY = s_YLow;// 这个位置是肯定不会有粒子的

// cout << "X = " << *pX << " ,Y = " << *pY << endl;

while (isAdjacent(*pX, *pY) == false)

{

    walk(pX, pY);

}

s_Grid[*pY][*pX] = 1;

return;

}

void setRange(int xRange, int yRange)

{

s_XRange = xRange;

s_YRange = yRange;



s_XLow = s_XCenter - xRange / 2;

s_YLow = s_YCenter - yRange / 2;

s_XHigh = s_XCenter + xRange / 2 - 1;

s_YHigh = s_YCenter + yRange / 2 - 1;

}

int main(void)

{

int i;

int x, y;

ofstream outfile;

outfile.open("information.txt");

//cerr << "#Hello world!" << endl;



srand(time(0));

//srand(0);

DLA_Init();



cerr << "stage 1" << endl;

setRange(200, 200);

for (i = 0; i < 1000; i++)

{

    DLA_Gen(&x, &y);

    outfile << x << "  " << y << endl;
    //cerr << i << endl;

}



cerr << "stage 2" << endl;

setRange(400, 400);

for (i = 0; i < 1000; i++)

{

    DLA_Gen(&x, &y);

    outfile << x << "  " << y << endl;

}



cerr << "stage 3" << endl;

setRange(600, 600);

for (i = 0; i < 5000; i++)

{

    DLA_Gen(&x, &y);

    outfile << x << "  " << y << endl;

}



cerr << "stage 4" << endl;

setRange(700, 700);

for (i = 0; i < 5000; i++)

{

    DLA_Gen(&x, &y);

    outfile << x << "  " << y << endl;

}

cerr << "stage 5" << endl;

setRange(900, 900);

for (i = 0; i < 5000; i++)

{

    DLA_Gen(&x, &y);

    outfile << x << "  " << y << endl;

}

cerr << "finished" << endl;

outfile.close();

return 0;

}

while (isAdjacent(*pX, *pY) == false)这里一直无法跳出,死循环了。
isAdjacent一直返回false,具体原因只能从你做的算法上差错了,为啥运行一定时间后,你这个函数就无法返回true了。

应该是你控制台输出的缓冲有限制,只能显示200行,你可以修改大一些。

void DLA_Gen(int *pX, int *pY)
{
//int i = 0; do

{

*pX = rand() % s_XRange + s_XLow;

*pY = rand() % s_YRange + s_YLow;

}

while (s_Grid[*pY][*pX] == 1);
最后这一句,之前py px位置已经遍历过设置成1了,你随机又到这个位置,就成了死循环,你这一个位置不能走两次 否则卡死,你可以在这直接return,
或者想个办法把=1的位置排除,前面那个的话不知道什么时候才能把所有位置设置成1,后面这个倒是可以