gpt4 book ai didi

c - 随机走过 10x10?

转载 作者:太空宇宙 更新时间:2023-11-04 05:40:19 26 4
gpt4 key购买 nike

这是我试图在 C 中解决的问题: http://i.stack.imgur.com/X70nX.png

我已经写了这段代码,但我不知道哪里出了问题。它没有给我任何输出,只是一个空屏幕! 1

这是我的代码:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 10
#define COL 10

#define RIGHT 0
#define UP 1
#define LEFT 2
#define DOWN 3

int main(void)
{
char mat[ROW][COL];
bool try;
int move, co, ro,letter;

//Filling the grid with "."
for (ro = 0; ro < ROW; ro++)
{
for (co = 0; co < COL; co++)
mat[ro][co] = '.';
}

//Initial Values
co = 0; ro = 0; mat[0][0] = 'A';


srand((unsigned)time(NULL));

for (letter = 1; letter < 26; letter++)
{
try = true;

while (try)
{
move = rand() % 4;

if ((move == RIGHT) && (co + 1 < COL) && (mat[ro][co+1]=='.'))
{
mat[ro][co + 1] = mat[ro][co] + 1;
co++; try=false;
}

if ((move == UP) && (ro - 1 >= 0) && (mat[ro-1][co]=='.') )
{
mat[ro - 1][co] = mat[ro][co] + 1;
ro--; try = false;
}

if ((move == LEFT) && (co - 1 >= 0) && (mat[ro][co-1]=='.'))
{
mat[ro][co - 1] = mat[ro][co] + 1;
co--; try = false;
}

if ((move == DOWN) && (ro + 1 < ROW) && (mat[ro+1][co]=='.'))
{
mat[ro + 1][co] = mat[ro][co] + 1;
ro++;
}
}
}


//Printing The GRID
for (ro = 0; ro < ROW; ro++)
{
for (co = 0; co < COL; co++)
printf(" %c", mat[ro][co]);
printf("\n");
}

return 0;
}

现在这里有关于相同问题的类似讨论:Random walk on 10x10 Array

但我仍然不知道我的代码有什么问题!!...请将我视为初学者。

修订:这是考虑到评论后的新版本:

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>
#define ROW 10
#define COL 10

#define RIGHT 0
#define UP 1
#define LEFT 2
#define DOWN 3

int main(void)
{
char mat[ROW][COL];
bool try;
int move, co, ro,letter,blocked;

//Filling the grid with "."
for (ro = 0; ro < ROW; ro++)
{
for (co = 0; co < COL; co++)
mat[ro][co] = '.';
}

//Initial Values
co = 0; ro = 0; mat[0][0] = 'A';


srand((unsigned)time(NULL));

for (letter = 1; letter < 26; letter++)
{
try = true;
blocked = 0;
while (try)
{
move = rand() % 4;
switch (move)
{

case RIGHT:
{ if ((co + 1 < COL) && (mat[ro][co + 1] == '.'))
{
mat[ro][co + 1] = mat[ro][co] + 1;
co++; try = false; break;
}
else { blocked++; break; }}

case UP:
{ if ((move == UP) && (ro - 1 >= 0) && (mat[ro - 1][co] == '.'))
{
mat[ro - 1][co] = mat[ro][co] + 1;
ro--; try = false; break;
}
else { blocked++; break; }}

case LEFT:
{ if ((move == LEFT) && (co - 1 >= 0) && (mat[ro][co - 1] == '.'))
{
mat[ro][co - 1] = mat[ro][co] + 1;
co--; try = false; break;
}
else { blocked++; break; }}

case DOWN:
{if ((move == DOWN) && (ro + 1 < ROW) && (mat[ro + 1][co] == '.'))
{
mat[ro + 1][co] = mat[ro][co] + 1;
ro++; try = false; break;
}
else { blocked++; break; }}
}
if (blocked == 4)
{try = false; letter=26;}
}

}


//Printing The GRID
for (ro = 0; ro < ROW; ro++)
{
for (co = 0; co < COL; co++)
printf(" %c", mat[ro][co]);
printf("\n");
}

return 0;
}

该代码仅有时有效,但不会在“Z”后停止(它应该是字母=26),

最佳答案

该代码有时 有效。我认为您经常会陷入无限循环,因为您会生成无法继续的随机游走。像这样:

A B C .
H I D .
G F E .
. . . .

关于c - 随机走过 10x10?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20730936/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com