weixin_39724080 2018-10-08 14:34 采纳率: 50%
浏览 506
已采纳

请问这个代码为什么每次只能运行一两百行就停止了?就是我的的数据输出来最多只能输出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;

}

  • 写回答

3条回答 默认 最新

  • 白色一大坨 2018-10-09 08:58
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?