#include
#include
#include
#define ROW 10
#define COL 10
#define RANDOM (rand() % 4) //取0,1,2,3的随机数
//每一步都从上下左右随机找一个能走的方向,将当前字母放在该位置,直到将所有大写字母放完,或者走到死胡同退出
int main(void)
{
char ch[ROW][COL];
int i=0,j=0,digit;
//初始化
for(int r=0; r
for(int c=0; c
ch[r][c] = '.';
srand((unsigned) time(NULL));
//循环遍历完所有26个大写字母
for(char a='A'; a<='Z'; a++){
//(i,j)位置一定是.,将当前字符a赋值到该位置
ch[i][j] = a;
//上下左右都不能走,退出,不过这里需要判断是否越界
if(ch[i-1][j] != '.' && ch[i+1][j] != '.' && ch[i][j-1] != '.' && ch[i][j+1] != '.')
break;
//do{}while循环的目的是找到一个能走的方向
do{
digit=1; //一个标志,如果后面得到的方向不能走通,继续判断下一个方向
switch(RANDOM){
case 0: //top
if(i-1 >= 0 && ch[i-1][j] == '.'){
i--;
digit=0;
}
break;
case 1: //down
if(i+1 < ROW && ch[i+1][j] == '.'){
i++;
digit=0;
}
break;
case 2: //left
if(j-1 >= 0 && ch[i][j-1] == '.'){
j--;
digit=0;
}
break;
case 3: //right
if(j+1 < COL && ch[i][j+1] == '.'){
j++;
digit=0;
}
break;
}
}while(digit != 0) //若digit==0表示找到能走的方向,退出循环,否则继续找
}
for(int i=0; i
for(int j=0; j
printf("%c ", ch[i][j]);
printf("\n");
}
return 0;
}