回复 8楼 wmf2014
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <time.h>
void chuShiHua(char qiPan[][12], bool panDuan[][12]);
int suiJiShu(void);
bool jianCha(bool panDuan[][12], char ziMu);
void yiDong(bool panDuan[][12], char qiPan[][12]);
void daYin(char qiPan[][12]);
int hang = 1, lie = 1;
char ziMu = 'A';
int main(void)
{
char qiPan[12][12];
bool panDuan[12][12];
srand((unsigned)time(NULL));
chuShiHua(qiPan, panDuan);
qiPan[1][1] = ziMu;
panDuan[1][1] = true;
while (jianCha(panDuan, ziMu))
yiDong(panDuan, qiPan);
daYin(qiPan);
return 0;
}
/* 初始化数组 */
void chuShiHua(char qiPan[][12], bool panDuan[][12])
{
for (int x = 0; x < 12; x++)
for (int y = 0; y < 12; y++)
qiPan[x][y] = '.';
for (int x = 0; x < 12; x++)
for (int y = 0; y < 12; y++)
if (x == 0 || y == 0 || x == 11 || y == 11)
panDuan[x][y] = true;
else
panDuan[x][y] = false;
}
/* 生成随机数 */
int suiJiShu(void)
{
return rand() % 4;
}
/* 判断,如果能继续则返回true;不能继续则返回false */
bool jianCha(bool panDuan[][12], char ziMu)
{
if (ziMu == 'Z')
return false;
if (panDuan[hang - 1][lie] && panDuan[hang + 1][lie])
if (panDuan[hang][lie - 1] && panDuan[hang][lie + 1])
return false;
return true;
}
/* 利用随机数进行方向的移动 */
void yiDong(bool panDuan[][12], char qiPan[][12])
{
bool zuobiao = true;
switch (suiJiShu())
{
case 0:hang--;
if (panDuan[hang][lie])
{
hang++;
zuobiao = false;
}
break;
case 1:lie++;
if (panDuan[hang][lie])
{
lie--;
zuobiao = false;
}
break;
case 2:hang++;
if (panDuan[hang][lie])
{
hang--;
zuobiao = false;
}
break;
case 3:lie--;
if (panDuan[hang][lie])
{
lie++;
zuobiao = false;
}
break;
}
if (zuobiao)
{
ziMu++;
qiPan[hang][lie] = ziMu;
panDuan[hang][lie] = true;
}
}
/* 打印 */
void daYin(char qiPan[][12])
{
for (int x = 1; x < 11; x++)
{
for (int y = 1; y < 11; y++)
putchar(qiPan[x][y]);
putchar('\n');
}
}
我成功了 不过不是声明的10×10数组 ,而是12×12